I am migrating an ASP.NET Web Form site to React + ASP.NET Core API.
In the ASP.NET Site, customers were subscribing to the system by filling a form in which they were entering in a field a unique URL for their profile (such as "mydomain.com/CustomerURLX"
).
The ASP.NET server script was creating a physical folder on the server, such as "mydomain.com/CustomerURLX"
, "mydomain.com/CustomerURLY"
. Once the folder was created, the script was also creating in that folder a single file called default.htm that was redirecting to ie:
"mydomain.com/loadCustomerProfile.aspx?ID=1"
"mydomain.com/loadCustomerProfile.aspx?ID=2"
The customers were using these URLs to access in a more friendly manner their profile on our web site.
All of the customers info are stored in a Database:
ClientID: 1 | MainURL: CustomerURLX
ClientID: 2 | MainURL: CustomerURLY
ClientID: 3 | MainURL: CustomerURLZ
etc.
So the question is, in React, would it be possible to query the database, then from the query result, to loop and create multiple Routes like "mydomain/CustomerURLX"
, etc. that would then redirect to "mydomain.com/loadProfile/ID"
? The database contains more than 20k customers rows (so more than 20k routes would be created), would it be a good practice in React regarding performance? Would it be better to create a local json file containing all customers URLS to create routes? Would it still be a good practice to create more than 20000 routes scanning a json file? Should I continue, like I was doing with ASP.NET, to create physical folders on the server for my redirect instead of using the React-Router? I am using [email protected]
.
2
Answers
I may have found a solution using the wildcard route from the react router, like: { path: '*', element: WildCardPath }
Then, the WildCardPath component would be like:
You don’t need to create a static route path in the app for each and every possible customer id value, this would be incredibly wasteful, difficult to read/maintain, and not efficient or DRY.
You should instead render a single route with a dynamic path.
Instead of this
Use this
or
Use the
useParams
hook in the routed component,Customer
in this example, to read the currentcustomerId
path parameter value.