Skip to content

Commit

Permalink
add support of dynamic assetPrefix in static pages
Browse files Browse the repository at this point in the history
  • Loading branch information
megazazik committed Jan 19, 2021
1 parent 01571fd commit 01cadd5
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = ({ dynamicAssetPrefix = false, ...nextConfig } = {}) => {
return Object.assign({}, nextConfig, {
publicRuntimeConfig: dynamicAssetPrefix
? Object.assign({}, nextConfig.publicRuntimeConfig, {
nextImagesAssetPrefix: nextConfig.assetPrefix
serverRuntimeConfig: dynamicAssetPrefix
? Object.assign({}, nextConfig.serverRuntimeConfig, {
nextImagesAssetPrefix: nextConfig.assetPrefix || nextConfig.basePath,
})
: nextConfig.publicRuntimeConfig,
: nextConfig.serverRuntimeConfig,
webpack(config, options) {
const { isServer } = options;
nextConfig = Object.assign({
Expand All @@ -31,14 +31,27 @@ module.exports = ({ dynamicAssetPrefix = false, ...nextConfig } = {}) => {
options: {
limit: nextConfig.inlineImageLimit,
fallback: require.resolve("file-loader"),
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath}/_next/static/images/`,
outputPath: `${isServer ? "../" : ""}static/images/`,
postTransformPublicPath: (p) => {
if (dynamicAssetPrefix && !nextConfig.assetPrefix) {
return `(require("next/config").default().publicRuntimeConfig.nextImagesAssetPrefix || '') + ${p}`
}
return p
},
...(dynamicAssetPrefix
? {
publicPath: `${
isServer ? '/_next/' : ''
}static/images/`,
postTransformPublicPath: (p) => {
if (isServer) {
return `(require("next/config").default().serverRuntimeConfig.nextImagesAssetPrefix || '') + ${p}`;
}

return `(__webpack_public_path__ || '') + ${p}`;
},
}
: {
publicPath: `${
nextConfig.assetPrefix ||
nextConfig.basePath ||
''
}/_next/static/images/`,
}),
name: "[name]-[hash].[ext]",
esModule: nextConfig.esModule || false
}
Expand Down

0 comments on commit 01cadd5

Please sign in to comment.