diff --git a/lib/asset.js b/lib/asset.js index 65b7e618..caa41b7e 100644 --- a/lib/asset.js +++ b/lib/asset.js @@ -7,7 +7,7 @@ export default function asset (path) { } const pathWithoutSlash = path.replace(/^\//, '') - return `${assetPrefix}/static/${pathWithoutSlash}` + return `${assetPrefix || ''}/static/${pathWithoutSlash}` } export function setAssetPrefix (url) { diff --git a/server/export.js b/server/export.js index 84050082..92f0aa43 100644 --- a/server/export.js +++ b/server/export.js @@ -8,6 +8,7 @@ import getConfig from './config' import { renderToHTML } from './render' import { getAvailableChunks } from './utils' import { printAndExit } from '../lib/utils' +import { setAssetPrefix } from '../lib/asset' export default async function (dir, options, configuration) { dir = resolve(dir) @@ -85,6 +86,9 @@ export default async function (dir, options, configuration) { availableChunks: getAvailableChunks(dir, config.distDir) } + // set the assetPrefix to use for 'next/asset' + setAssetPrefix(renderOpts.assetPrefix) + // We need this for server rendering the Link component. global.__NEXT_DATA__ = { nextExport: true diff --git a/server/index.js b/server/index.js index b11b2494..80b41f9c 100644 --- a/server/index.js +++ b/server/index.js @@ -85,7 +85,7 @@ export default class Server { } setAssetPrefix (prefix) { - this.renderOpts.assetPrefix = prefix.replace(/\/$/, '') + this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : '' asset.setAssetPrefix(this.renderOpts.assetPrefix) } diff --git a/test/integration/custom-server/server.js b/test/integration/custom-server/server.js index 8547f14a..447cc4d0 100644 --- a/test/integration/custom-server/server.js +++ b/test/integration/custom-server/server.js @@ -12,6 +12,8 @@ app.prepare().then(() => { const server = micro((req, res) => { if (/setAssetPrefix/.test(req.url)) { app.setAssetPrefix(`http://127.0.0.1:${port}`) + } else if (/setEmptyAssetPrefix/.test(req.url)) { + app.setAssetPrefix(null) } else { // This is to support multi-zones support in localhost // and may be in staging deployments diff --git a/test/integration/custom-server/test/index.test.js b/test/integration/custom-server/test/index.test.js index e476d766..92b60631 100644 --- a/test/integration/custom-server/test/index.test.js +++ b/test/integration/custom-server/test/index.test.js @@ -38,6 +38,11 @@ describe('Custom Server', () => { expect(dynamicUsage).toMatch(/127\.0\.0\.1/) }) + it('should handle null assetPrefix accordingly', async () => { + const $normal = cheerio.load(await renderViaHTTP(appPort, '/asset?setEmptyAssetPrefix=1')) + expect($normal('img').attr('src')).toBe('/static/myimage.png') + }) + it('should set the assetPrefix to a given request', async () => { for (let lc = 0; lc < 1000; lc++) { const [normalUsage, dynamicUsage] = await Promise.all([ diff --git a/test/integration/static/next.config.js b/test/integration/static/next.config.js index dedaf9f5..96c6be7e 100644 --- a/test/integration/static/next.config.js +++ b/test/integration/static/next.config.js @@ -3,6 +3,7 @@ module.exports = { return { '/': { page: '/' }, '/about': { page: '/about' }, + '/asset': { page: '/asset' }, '/button-link': { page: '/button-link' }, '/get-initial-props-with-no-query': { page: '/get-initial-props-with-no-query' }, '/counter': { page: '/counter' }, diff --git a/test/integration/static/pages/asset.js b/test/integration/static/pages/asset.js new file mode 100644 index 00000000..92b695bb --- /dev/null +++ b/test/integration/static/pages/asset.js @@ -0,0 +1,7 @@ +import asset from 'next/asset' + +export default () => ( +
+ +
+) diff --git a/test/integration/static/test/ssr.js b/test/integration/static/test/ssr.js index b08ebbb3..0b060fe9 100644 --- a/test/integration/static/test/ssr.js +++ b/test/integration/static/test/ssr.js @@ -42,5 +42,11 @@ export default function (context) { const html = await renderViaHTTP(context.port, '/get-initial-props-with-no-query') expect(html).toMatch(/Query is: {}/) }) + + it('should handle next/asset properly', async () => { + const html = await renderViaHTTP(context.port, '/asset') + const $ = cheerio.load(html) + expect($('img').attr('src')).toBe('/static/myimage.png') + }) }) } diff --git a/test/node_modules/next b/test/node_modules/next new file mode 120000 index 00000000..6581736d --- /dev/null +++ b/test/node_modules/next @@ -0,0 +1 @@ +../../ \ No newline at end of file