fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Preserve assetPrefix in asPath #265

Closed bbourn closed 1 year ago

bbourn commented 5 years ago

We are using assetPrefix and have next setup to serve on a sub-folder. On initial load our pages router object has: asPath:"/prefix-dir/Homepage/[old param name]" pathname and route are both "/homepage" query has [old param name] And page load as it should. Everything works fine.

Then we navigate to a new by calling: Router.pushRoute("/Homepage/[new param name]") And now that same pages router object has: asPath:"/Homepage/[new param name]" <---- without the needed /prefix-dir pathname and route are both "/homepage" query has [new param name]

How can I preserve the assetPrefix in the asPath? If I put it in the Router.pushRoute("/prefix-dir/Homepage/[new param name]") statement, I get net::ERR_ABORTED 404 on the _next/static/development/pages/pages/prefix-dir/Homepage/[new param name].js url because there isn't a sub-dir in pages, and the parameter isn't a page.

If I use Router.pushRoute("/Homepage/[new param name]") everything is fine, accept if they hit refresh on the page, the get an 'Cannot GET /Homepage' error because that page doesn't exist without the /prefix-dir.

If I leave off the beginning '/' I get: Uncaught (in promise) Error: Route name should start with a "/",

How can I get Router.pushRoute to set asPath with the prefix-dir prepended?

bbourn commented 5 years ago

So right now I am using the pushRoute('/prefix-dir/Homepage/') syntax as it errors out, then loads the page anyway (shallow) with the proper url... Which works in chrome for now but not in Safari.

LaurentVB commented 5 years ago

I was having the same issue as you, and found this comment to offer a viable workaround: https://github.com/fridays/next-routes/issues/30#issuecomment-383368319

bbourn commented 5 years ago

@LaurentVBk, @stanleyfok THANK YOU SO MUCH!!! That worked beautifully!