Next.js: fail fetching getServerSideProps with basePath

Created on 28 Jul 2020  路  6Comments  路  Source: vercel/next.js

Bug report

Describe the bug

next 9.5.0 issue

After I setuped next.config.js like below,

assetPrefix: "/resources/20200728145115"
basePath: "/ssr"

when I tried to change path by router,
fetching getServerSideProps was failed with 404 error.

So I checked data fetch url, it contains assetPrefix instead of basePath.
/resources/20200728145115/_next/data/LzktM7sKix9_y5d3dr7ph/[pageRoute]

https://github.com/vercel/next.js/blob/d33dbea8dc684003a8d7babce58b60140089c6e1/packages/next/next-server/lib/router/router.ts#L696-L702

https://github.com/vercel/next.js/blob/d33dbea8dc684003a8d7babce58b60140089c6e1/packages/next/client/page-loader.js#L114-L127

System information

Version of Next.js: 9.5.0
Version of Node.js: 10.13.0

bug p0 next

Most helpful comment

I'm using assetPrefix for serving js/css from a different host than the Next.js server is running on.
Since getServerSideProps is executed for each request I wouldn't expect this to be served from the same location as static files.

For example:

basePath: "/basepath",
assetPrefix: "https://mycdn.example.com/app_path/"

The user visits https://myapp.example.com/basepath/page
Which loads js like this as expected https://mycdn.example.com/app_path/_next/static/chunks/pages/(...).js
And I would expect getServerSideProps requests to look like this: https://myapp.example.com/basepath/_next/data/(...).json
But instead they end up looking like this: https://mycdn.example.com/app_path/_next/data/(...).json

Since mycdn.example.com is just serving a folder with staticfiles this can't work.

In #11992 it looks like people are trying to use assetPrefix as basePath IMO which kinda makes sense as basePath support was just released.

All 6 comments

This behavior was changed as a bug fix, requested in https://github.com/vercel/next.js/issues/11992. Could you please explain what your assetPrefix is being used for and your use case more?

I'm using assetPrefix for serving js/css from a different host than the Next.js server is running on.
Since getServerSideProps is executed for each request I wouldn't expect this to be served from the same location as static files.

For example:

basePath: "/basepath",
assetPrefix: "https://mycdn.example.com/app_path/"

The user visits https://myapp.example.com/basepath/page
Which loads js like this as expected https://mycdn.example.com/app_path/_next/static/chunks/pages/(...).js
And I would expect getServerSideProps requests to look like this: https://myapp.example.com/basepath/_next/data/(...).json
But instead they end up looking like this: https://mycdn.example.com/app_path/_next/data/(...).json

Since mycdn.example.com is just serving a folder with staticfiles this can't work.

In #11992 it looks like people are trying to use assetPrefix as basePath IMO which kinda makes sense as basePath support was just released.

Same case as @duvholt.
I expected that getServerSideProps fetching url have to contain basePath, not assetPrefix.

assetPrefix is added for cdn static files like js/css.

Also have same use case as @duvholt . Is the json data something I should serve from CDN as a short-term stopgap?

This was just fixed in 9.5.1, please upgrade!

Thanks @Janpot and @Timer. We used the fix in 9.5.1-canary.2 early today and it worked as expected, will upgrade to 9.5.1 shortly

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renatorib picture renatorib  路  3Comments

formula349 picture formula349  路  3Comments

jesselee34 picture jesselee34  路  3Comments

sospedra picture sospedra  路  3Comments

pie6k picture pie6k  路  3Comments