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: fix encoding issues with payload paths #8738

Merged
merged 10 commits into from
Feb 2, 2021
2 changes: 1 addition & 1 deletion packages/generator/src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export default class Generator {
// Save Static Assets
if (this.staticAssetsDir && renderContext.staticAssets) {
for (const asset of renderContext.staticAssets) {
const assetPath = path.join(this.staticAssetsDir, asset.path)
const assetPath = path.join(this.staticAssetsDir, decodeURI(asset.path))
await fsExtra.ensureDir(path.dirname(assetPath))
await fsExtra.writeFile(assetPath, asset.src, 'utf-8')
}
Expand Down
8 changes: 4 additions & 4 deletions packages/vue-app/template/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { parsePath, withoutTrailingSlash } from 'ufo'
import { parsePath, withoutTrailingSlash, normalizeURL } from 'ufo'
<% utilsImports = [
...(features.asyncData || features.fetch) ? [
'getMatchedComponentsInstances',
Expand Down Expand Up @@ -317,7 +317,7 @@ export default {
},
<% if (nuxtOptions.generate.manifest) { %>
async fetchStaticManifest() {
return window.__NUXT_IMPORT__('manifest.js', encodeURI(urlJoin(this.getStaticAssetsPath(), 'manifest.js')))
return window.__NUXT_IMPORT__('manifest.js', normalizeURL(urlJoin(this.getStaticAssetsPath(), 'manifest.js')))
},
<% } %>
setPagePayload(payload) {
Expand All @@ -328,14 +328,14 @@ export default {
<% if (nuxtOptions.generate.manifest) { %>
const manifest = await this.fetchStaticManifest()
const path = this.getRoutePath(route)
if (!manifest.routes.includes(path)) {
if (!manifest.routes.includes(decodeURI(path))) {
if (!prefetch) { this.setPagePayload(false) }
throw new Error(`Route ${path} is not pre-rendered`)
}
<% } %>
const src = urlJoin(this.getStaticAssetsPath(route), 'payload.js')
try {
const payload = await window.__NUXT_IMPORT__(decodeURI(route), encodeURI(src))
const payload = await window.__NUXT_IMPORT__(decodeURI(route), normalizeURL(src))
if (!prefetch) { this.setPagePayload(payload) }
return payload
} catch (err) {
Expand Down
19 changes: 17 additions & 2 deletions test/dev/full-static.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import http from 'http'
import { resolve } from 'path'
import { join, resolve } from 'path'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import glob from 'glob'
import { Builder, Generator, getPort, loadFixture, Nuxt, rp } from '../utils'

let port
Expand Down Expand Up @@ -43,7 +44,21 @@ describe('full-static', () => {
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/')
expect(html).toContain(
'<link rel="preload" href="https://cdn.nuxtjs.org/test/_nuxt/static/'
)
})

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

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
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/full-static/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<NLink to="/store">
Store
</NLink>
<NLink to="/encoding/中文">
Encoding
</NLink>
<NLink to="/pagination/1">
Pagination
</NLink>
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/full-static/pages/encoding/中文.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<h1>Encoded path</h1>
</div>
</template>