mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
fix(vue-renderer): respect injectScripts for target:static (#8912)
This commit is contained in:
parent
880fe4abd7
commit
07e97f168a
@ -177,7 +177,7 @@ export default class SSRRenderer extends BaseRenderer {
|
|||||||
const shouldHashCspScriptSrc = csp && (csp.unsafeInlineCompatibility || !containsUnsafeInlineScriptSrc)
|
const shouldHashCspScriptSrc = csp && (csp.unsafeInlineCompatibility || !containsUnsafeInlineScriptSrc)
|
||||||
const inlineScripts = []
|
const inlineScripts = []
|
||||||
|
|
||||||
if (renderContext.staticAssetsBase) {
|
if (shouldInjectScripts && renderContext.staticAssetsBase) {
|
||||||
const preloadScripts = []
|
const preloadScripts = []
|
||||||
renderContext.staticAssets = []
|
renderContext.staticAssets = []
|
||||||
const { staticAssetsBase, url, nuxt, staticAssets } = renderContext
|
const { staticAssetsBase, url, nuxt, staticAssets } = renderContext
|
||||||
|
@ -14,55 +14,76 @@ let builder
|
|||||||
let server = null
|
let server = null
|
||||||
let generator = null
|
let generator = null
|
||||||
|
|
||||||
|
const generateAndStartServer = async (overrides) => {
|
||||||
|
const config = await loadFixture('full-static', {
|
||||||
|
generate: {
|
||||||
|
static: false,
|
||||||
|
dir: '.nuxt-generate'
|
||||||
|
},
|
||||||
|
...(overrides || {})
|
||||||
|
})
|
||||||
|
const nuxt = new Nuxt(config)
|
||||||
|
await nuxt.ready()
|
||||||
|
|
||||||
|
builder = new Builder(nuxt)
|
||||||
|
builder.build = jest.fn()
|
||||||
|
generator = new Generator(nuxt, builder)
|
||||||
|
|
||||||
|
await generator.generate()
|
||||||
|
|
||||||
|
const serve = serveStatic(distDir)
|
||||||
|
server = http.createServer((req, res) => {
|
||||||
|
serve(req, res, finalhandler(req, res))
|
||||||
|
})
|
||||||
|
|
||||||
|
port = await getPort()
|
||||||
|
server.listen(port)
|
||||||
|
}
|
||||||
|
|
||||||
describe('full-static', () => {
|
describe('full-static', () => {
|
||||||
beforeAll(async () => {
|
describe('with scripts', () => {
|
||||||
const config = await loadFixture('full-static', {
|
beforeAll(async () => await generateAndStartServer())
|
||||||
generate: {
|
|
||||||
static: false,
|
|
||||||
dir: '.nuxt-generate'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
const nuxt = new Nuxt(config)
|
|
||||||
await nuxt.ready()
|
|
||||||
|
|
||||||
builder = new Builder(nuxt)
|
test('/payload (custom build.publicPath)', async () => {
|
||||||
builder.build = jest.fn()
|
const { body: html } = await rp(url('/payload'))
|
||||||
generator = new Generator(nuxt, builder)
|
|
||||||
|
|
||||||
await generator.generate()
|
expect(html).toContain('<script src="https://cdn.nuxtjs.org/test/')
|
||||||
|
expect(html).toContain(
|
||||||
const serve = serveStatic(distDir)
|
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
|
||||||
server = http.createServer((req, res) => {
|
)
|
||||||
serve(req, res, finalhandler(req, res))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
port = await getPort()
|
test('/encoding/中文', async () => {
|
||||||
server.listen(port)
|
const { body: html } = await rp(url('/encoding/中文'))
|
||||||
})
|
|
||||||
|
|
||||||
test('/payload (custom build.publicPath)', async () => {
|
const paths = ['encoding/中文/state.js', 'encoding/中文/payload.js']
|
||||||
const { body: html } = await rp(url('/payload'))
|
|
||||||
|
|
||||||
expect(html).toContain('<script src="https://cdn.nuxtjs.org/test/')
|
paths.forEach((path) => {
|
||||||
expect(html).toContain(
|
const files = glob.sync(join(distDir, '**', path))
|
||||||
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
|
expect(html).toContain(encodeURI(path))
|
||||||
)
|
expect(files).toContainEqual(expect.stringContaining(path))
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('/encoding/中文', async () => {
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
const { body: html } = await rp(url('/encoding/中文'))
|
afterAll(async () => {
|
||||||
|
await server.close()
|
||||||
const paths = ['encoding/中文/state.js', 'encoding/中文/payload.js']
|
|
||||||
|
|
||||||
paths.forEach((path) => {
|
|
||||||
const files = glob.sync(join(distDir, '**', path))
|
|
||||||
expect(html).toContain(encodeURI(path))
|
|
||||||
expect(files).toContainEqual(expect.stringContaining(path))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// Close server and ask nuxt to stop listening to file changes
|
describe('without scripts', () => {
|
||||||
afterAll(async () => {
|
beforeAll(async () => await generateAndStartServer({ render: { injectScripts: false } }))
|
||||||
await server.close()
|
|
||||||
|
test('should not inject scripts', async () => {
|
||||||
|
const { body: html } = await rp(url('/payload'))
|
||||||
|
|
||||||
|
expect(html).not.toContain('<script')
|
||||||
|
expect(html).not.toContain('<link')
|
||||||
|
})
|
||||||
|
|
||||||
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
|
afterAll(async () => {
|
||||||
|
await server.close()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user