Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-renderer): respect injectScripts for target:static #8912

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vue-renderer/src/renderers/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class SSRRenderer extends BaseRenderer {
const shouldHashCspScriptSrc = csp && (csp.unsafeInlineCompatibility || !containsUnsafeInlineScriptSrc)
const inlineScripts = []

if (renderContext.staticAssetsBase) {
if (shouldInjectScripts && renderContext.staticAssetsBase) {
const preloadScripts = []
renderContext.staticAssets = []
const { staticAssetsBase, url, nuxt, staticAssets } = renderContext
Expand Down
95 changes: 58 additions & 37 deletions test/dev/full-static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,76 @@ let builder
let server = null
let generator = null

describe('full-static', () => {
beforeAll(async () => {
const config = await loadFixture('full-static', {
generate: {
static: false,
dir: '.nuxt-generate'
}
})
const nuxt = new Nuxt(config)
await nuxt.ready()
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)

builder = new Builder(nuxt)
builder.build = jest.fn()
generator = new Generator(nuxt, builder)
await generator.generate()

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('with scripts', () => {
beforeAll(async () => await generateAndStartServer())

const serve = serveStatic(distDir)
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))
test('/payload (custom build.publicPath)', async () => {
const { body: html } = await rp(url('/payload'))

expect(html).toContain('<script src="https://cdn.nuxtjs.org/test/')
expect(html).toContain(
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
)
})

port = await getPort()
server.listen(port)
})
test('/encoding/中文', async () => {
const { body: html } = await rp(url('/encoding/中文'))

test('/payload (custom build.publicPath)', async () => {
const { body: html } = await rp(url('/payload'))
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))
})
})

expect(html).toContain('<script src="https://cdn.nuxtjs.org/test/')
expect(html).toContain(
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
)
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await server.close()
})
})

test('/encoding/中文', async () => {
const { body: html } = await rp(url('/encoding/中文'))
describe('without scripts', () => {
beforeAll(async () => await generateAndStartServer({ render: { injectScripts: false } }))

const paths = ['encoding/中文/state.js', 'encoding/中文/payload.js']
test('should not inject scripts', async () => {
const { body: html } = await rp(url('/payload'))

paths.forEach((path) => {
const files = glob.sync(join(distDir, '**', path))
expect(html).toContain(encodeURI(path))
expect(files).toContainEqual(expect.stringContaining(path))
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()
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await server.close()
})
})
})