From e7c1edadc8c7ba0ecd5bde977cffc9813cb1b237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20J=C3=B3zsa?= Date: Thu, 24 Sep 2020 10:37:26 +0200 Subject: [PATCH] Add support for dynamic assetPrefix via next runtime configuration utils. --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 999e0ab..652e3d5 100644 --- a/index.js +++ b/index.js @@ -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({ @@ -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 }