Skip to content

Commit

Permalink
Merge pull request #44 from peter-jozsa/feature/dynamic-asset-prefix
Browse files Browse the repository at this point in the history
Dynamic assetPrefix via next runtime configuration utils
  • Loading branch information
arefaslani authored Sep 25, 2020
2 parents 3ec4620 + e7c1eda commit 932ca6b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = (nextConfig = {}) => {
return Object.assign({}, nextConfig, {
publicRuntimeConfig: Object.assign({}, nextConfig.publicRuntimeConfig, {
nextImagesAssetPrefix: nextConfig.assetPrefix
}),
webpack(config, options) {
const { isServer } = options;
nextConfig = Object.assign({
Expand Down Expand Up @@ -28,6 +31,13 @@ module.exports = (nextConfig = {}) => {
fallback: require.resolve("file-loader"),
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath}/_next/static/images/`,
outputPath: `${isServer ? "../" : ""}static/images/`,
postTransformPublicPath: (p) => {
if (!nextConfig.assetPrefix) {
return `(require("next/config").default().publicRuntimeConfig.nextImagesAssetPrefix || '') + ${p}`
}
// no need to dynamically prepend assetPrefix to p since it was defined in transpilation-time
return p
},
name: "[name]-[hash].[ext]",
esModule: nextConfig.esModule || false
}
Expand Down

0 comments on commit 932ca6b

Please sign in to comment.