-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use content hash in pages chunk name #6303
Comments
Aha~! I am planning to submit an issue like this. This is also a problem that has been bothering me for a long time. As @wbr911 said, this wastes a lot of resources and greatly reduces development and user experience. @timneutkens |
It's slightly more complicated than saying "I want to add hashes", the problem space is bigger than that. For example how are you going to route? The client side is not aware of the hashes for the script tag, and it shouldn't have a list of all possible routes + hashes (that wouldn't scale). I've just created a flow chart that (from the top of my mind) explains how the router currently works: This issue actually comes really close to the dynamic routing one, because there needs to be a way to specify patterns to a certain page. Adding hashes would be super simple if for example we were to do something like: import Link, {page} from 'next/link'
export default function MyPage() {
return <>
<Link href={page`./blog/post.js`} as="/blog/hello-world">
<a>To Hello World Blog Post</a>
</Link>
</>
} Because in that case we can compile the href into the hashed page route. Another one that came up when thinking about this is: import Link, {page} from 'next/link'
export default function MyPage() {
return <>
<Link href={() => import('./blog/post.js')} as="/blog/hello-world">
<a>To Hello World Blog Post</a>
</Link>
</>
} Note that this is just me brainstorming on solutions, this is not something we're currently implementing (still thinking more about it). |
Thank @timneutkens for your detailed answers, and I agree that this feature is difficult to achieve. Maybe we have to alleviate this problem by dividing complex projects into multiple next.js apps. |
@timneutkens First of All , Thanks a lot for your patient explanation |
@wbr911 could expand on this more, do you mean you're not using |
yes @timneutkens |
There is no way for us to distinguish between that, so the solution has to be generic and work for all cases. |
could you let developer do it, for example to add a configuration in next.config.js to declare whether use contentHash and remove versionId in the path of page.js @timneutkens |
It's not a simple as changing a configuration. We can't maintain 2 versions of page loading and building just for this one use case. |
just to share my solution to people sharing same situation with us:
|
So I got the custom
But this is very much tied with implementation details of next.js and is very fragile and can break with next.js versions. I think if next.js would be open to a few changes, then just a plugin to modify the asset filename can become very easy:
@timneutkens if you would be ok with this I can create a PR. Then next-plugin creators like me can easily modify the asset pathname from webpack. |
Actually it's not easy, because it implies you know all routes + it doesn't account for dynamic |
Can you share your solution that you are working on or if there is a PR or a task list, maybe we can contribute? |
I'm still discussing it internally before writing up something. |
Ok so I got my implementation working extending NextScript and patching PageLoader. Diff: https://github.com/azizhk/next-typescript-mdx-emotion/pull/1/files Demo: https://5c97db3e78a9f70008989219--next-ts-mdx-emotion.netlify.com/ |
Posted #7329 |
Correct me if I'm wrong. What's the issue of storing the page->hash map on the client side and do a lookup during page transition when via A middle ground IMO is to generate a BUILD ID that's a total hash of all frontend source code. This helps in my case, to not invalidate frontend cache when there's only backend changes. Do you guys think it's reasonable to do this? |
@ZenoZen yup it's possible. I've documented the process https://medium.com/@azizhk/next-js-netlify-file-naming-and-caching-strategies-8665b213963f |
@azizhk Yeah, I've seen your blog post. It's really helpful! The process sounds just right, with the asset hash mapping dictionary. But as you said, the hack is a bit too involved and likely to break when an official solution comes up. That's why I'm considering the single total hash solution and hopefully we don't have to wait long for an official solution :) |
I would highly recommend not using the outlined solution, it will break for every single release we'll do, especially the ones that will be out soon. |
@timneutkens Is there any update on this issue? A small change cause all js need to be downloaded again on user side seems not elegant, especially for big sites. I'm eager for the solution. |
@timneutkens also asking for an update on this. Since we often have multiple deploys of the same nextjs applications per week or even per day, we are constantly invalidating caches, even from pages, that didn't change. A more user focused solution on this problem would be great. |
We're making some changes soon, but will take a while as it's non-trivial to make it work. Feel free to reach out to [email protected] if you want us working on it in a shorter timeframe. |
Updates the way filenames are generated for browser compilation. Notably: - All entry bundles now have hashes in production, this includes pages (previously pages used a buildId in the path) - The AmpFiles no longer depends on hardcoded bundle names, it uses the buildManifest instead (internals) - All cases where we match the page name from the chunk/entrypoint name now use the same function `getRouteFromEntrypoint` (internals) - In development we no longer include the "faked" `buildId` set to `development` for page files, instead we just use the `/_next/static/pages` path (was `/_next/static/development/pages`). This was changed as it caused unneeded complexity and makes generating the bundles easier (internals) - Updated tons of tests to be more resilient to these changes by relying on the buildManifest instead of hardcoded paths (internals) Follow up of these PRs: #13759 #13870 #13937 #14130 #14176 #14268 Fixes #6303 Fixes #12087 Fixes #1948 Fixes #4368 Fixes #4255 Fixes #2548
Updates the way filenames are generated for browser compilation. Notably: - All entry bundles now have hashes in production, this includes pages (previously pages used a buildId in the path) - The AmpFiles no longer depends on hardcoded bundle names, it uses the buildManifest instead (internals) - All cases where we match the page name from the chunk/entrypoint name now use the same function `getRouteFromEntrypoint` (internals) - In development we no longer include the "faked" `buildId` set to `development` for page files, instead we just use the `/_next/static/pages` path (was `/_next/static/development/pages`). This was changed as it caused unneeded complexity and makes generating the bundles easier (internals) - Updated tons of tests to be more resilient to these changes by relying on the buildManifest instead of hardcoded paths (internals) Follow up of these PRs: vercel#13759 vercel#13870 vercel#13937 vercel#14130 vercel#14176 vercel#14268 Fixes vercel#6303 Fixes vercel#12087 Fixes vercel#1948 Fixes vercel#4368 Fixes vercel#4255 Fixes vercel#2548
FYI - this feature was a part of v9.5 release. |
Are there any plans to add a content hash to files from the public folder, like images and fonts? |
It's important to have something like content hash in their name, so we can use |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Feature request
Is your feature request related to a problem? Please describe.
for entries under pages folder , nextJs will not add
content_hash
to entries' name. Instead the path will be__next/$nextVersion/page/$pageName.js.
As a result , each time when we make a new release , nextJs will generate a new version, and all the path of pages js will be changed and in turn the
web cache is gone.
For example, if we have 10 page , and we only modify one page in next release , but all the 10 page js need to be downloaded again.Describe the solution you'd like
use content hash in page name
__next/$nextVersion/page/$pageName.js.
=>__next/page/$pageName_$contentHash.js
The text was updated successfully, but these errors were encountered: