mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Handle empty assetPrefix scenario properly. (#3667)
Also make sure to init assetPrefix in next/asset in the context of next-export.
This commit is contained in:
parent
7afc008aa7
commit
373661aafa
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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([
|
||||
|
|
|
@ -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' },
|
||||
|
|
7
test/integration/static/pages/asset.js
Normal file
7
test/integration/static/pages/asset.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
import asset from 'next/asset'
|
||||
|
||||
export default () => (
|
||||
<div id='asset-page'>
|
||||
<img src={asset('/myimage.png')} />
|
||||
</div>
|
||||
)
|
|
@ -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')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
1
test/node_modules/next
generated
vendored
Symbolic link
1
test/node_modules/next
generated
vendored
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../
|
Loading…
Reference in a new issue