question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support dynamic assetPrefix

See original GitHub issue

Currently 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:closed
  • Created 5 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
maciejmajewskicommented, Nov 6, 2019

So FYI I was able to make it work with following config:

publicPath: `${isServer ? "/_next/" : ""}static/images/`,
outputPath: "static/images/",
postTransformPublicPath: (p) => {
  if (isServer) {
    return `(process.env.ASSET_PREFIX || '') + ${p}`;
  } else {
    return `__webpack_public_path__ + ${p}`
  }
},

postTransformPublicPath is available in file-loader from version 4.2.0: https://github.com/webpack-contrib/file-loader/blob/master/CHANGELOG.md#420-2019-08-07


Edit: 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.

1reaction
arefaslanicommented, Feb 26, 2019

@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 of assetPrefix config in the Webpack file equal to this env variable. I mean something like

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  inlineImageLimit: 16384,
  assetPrefix: process.env.ASSET_PREFIX,
  webpack(config, options) {
    return config
  }
})

You can reopen this issue if you think this issue can be solved in a better way.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found