Support dynamic assetPrefix
See original GitHub issueCurrently this plugin only works if assetPrefix
is set at build step. (meaning it only support a static CDN)
This is quite hard to get working when considering apps running on multiple environments; where each environment could have a different CDN (like a production CDN vs a staging CDN.)
Furthermore, Next.js expose many ways to make the assetPrefix dynamic (like switching CDN domain based on the app domain, etc.) By requiring a static CDN, this plugin cannot support any of those use cases.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
CDN Support with Asset Prefix - next.config.js
A custom asset prefix allows you serve static assets from a CDN. Learn more about it here.
Read more >next.config.js: CDN Support with Asset Prefix
A custom asset prefix allows you serve static assets from a CDN. Learn more about it here.
Read more >Customize assetPrefix in `next/dynamic` import - Stack Overflow
I am using next/dynamic to dynamically import a component like this: import dynamic from 'next/dynamic'; const Foo = dynamic(() ...
Read more >Next-server NPM - npm.io
Note: Predefined routes take precedence over dynamic routes. ... Server((req, res) => { // Add assetPrefix support based on the hostname if ...
Read more >Static Asset Handling - Vite
This pattern also supports dynamic URLs via template literals: js function getImageUrl(name) { return new URL(`./dir/${name}.png`, import.meta.url).href }.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So FYI I was able to make it work with following config:
postTransformPublicPath
is available infile-loader
from version4.2.0
: https://github.com/webpack-contrib/file-loader/blob/master/CHANGELOG.md#420-2019-08-07Edit: to clarify, this sample helps set proper public path when SSR doesn’t have proper value for
__webpack_public_path__
, it does not solve issues with having changing public path according to request.@SBoudrias I don’t think that it’s possible to use dynamic imports in Webpack configuration as in the Next app. Because Next sets the asset prefix in the app level but we can’t do that when defining Webpack loaders. I think the best way for doing this is by using an env variable like
process.env.ASSET_PREFIX
and set the value ofassetPrefix
config in the Webpack file equal to this env variable. I mean something likeYou can reopen this issue if you think this issue can be solved in a better way.