fix(vue-app): add leading slash for payload hydration (#8802)

This commit is contained in:
Daniel Roe 2021-02-10 12:18:27 +00:00 committed by GitHub
parent c221f60780
commit 9d33456e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 15 deletions

View File

@ -18,7 +18,7 @@
"lodash": "^4.17.20", "lodash": "^4.17.20",
"rc9": "^1.2.0", "rc9": "^1.2.0",
"std-env": "^2.2.1", "std-env": "^2.2.1",
"ufo": "^0.6.5" "ufo": "^0.6.6"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"

View File

@ -16,7 +16,7 @@
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"node-html-parser": "^2.1.0", "node-html-parser": "^2.1.0",
"ufo": "^0.6.5" "ufo": "^0.6.6"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"

View File

@ -24,7 +24,7 @@
"serve-placeholder": "^1.2.3", "serve-placeholder": "^1.2.3",
"serve-static": "^1.14.1", "serve-static": "^1.14.1",
"server-destroy": "^1.0.1", "server-destroy": "^1.0.1",
"ufo": "^0.6.5" "ufo": "^0.6.6"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"

View File

@ -19,7 +19,7 @@
"serialize-javascript": "^5.0.1", "serialize-javascript": "^5.0.1",
"signal-exit": "^3.0.3", "signal-exit": "^3.0.3",
"ua-parser-js": "^0.7.23", "ua-parser-js": "^0.7.23",
"ufo": "^0.6.5" "ufo": "^0.6.6"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"

View File

@ -14,7 +14,7 @@
], ],
"dependencies": { "dependencies": {
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"ufo": "^0.6.5", "ufo": "^0.6.6",
"unfetch": "^4.2.0", "unfetch": "^4.2.0",
"vue": "^2.6.12", "vue": "^2.6.12",
"vue-client-only": "^2.0.0", "vue-client-only": "^2.0.0",

View File

@ -1,5 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
import { parsePath, withoutTrailingSlash, normalizeURL } from 'ufo' import { decode, parsePath, withoutBase, withoutTrailingSlash, normalizeURL } from 'ufo'
<% utilsImports = [ <% utilsImports = [
...(features.asyncData || features.fetch) ? [ ...(features.asyncData || features.fetch) ? [
'getMatchedComponentsInstances', 'getMatchedComponentsInstances',
@ -301,10 +301,7 @@ export default {
}, },
getRoutePath(route = '/') { getRoutePath(route = '/') {
const base = this.getRouterBase() const base = this.getRouterBase()
if (base && route.startsWith(base)) { return withoutTrailingSlash(withoutBase(parsePath(route).pathname, base))
route = route.substr(base.length)
}
return withoutTrailingSlash(parsePath(route).pathname)
}, },
getStaticAssetsPath(route = '/') { getStaticAssetsPath(route = '/') {
const { staticAssetsBase } = window.<%= globals.context %> const { staticAssetsBase } = window.<%= globals.context %>

View File

@ -14,7 +14,7 @@
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
"lodash": "^4.17.20", "lodash": "^4.17.20",
"lru-cache": "^5.1.1", "lru-cache": "^5.1.1",
"ufo": "^0.6.5", "ufo": "^0.6.6",
"vue": "^2.6.12", "vue": "^2.6.12",
"vue-meta": "^2.4.0", "vue-meta": "^2.4.0",
"vue-server-renderer": "^2.6.12" "vue-server-renderer": "^2.6.12"

View File

@ -0,0 +1,64 @@
import http from 'http'
import { resolve } from 'path'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import { Builder, Generator, getPort, loadFixture, Nuxt } from '../utils'
import renderAndGetWindow from '../../packages/server/src/jsdom'
let port
const url = route => 'http://localhost:' + port + route
let builder
let server = null
let generator = null
const fixtures = [
['trailing-slash/with-true', '/posts/test/'],
['trailing-slash/with-false', '/posts/test'],
['trailing-slash/with-default', '/posts/test'],
['trailing-slash/with-default', '/posts/test/']
]
fixtures.forEach(([fixture, path]) => {
describe(`trailing-slash payloads (${fixture})`, () => {
beforeAll(async () => {
const dir = `.nuxt-generate-${fixture.replace('/', '_')}`
const rootDir = resolve(__dirname, '../fixtures', fixture)
const distDir = resolve(rootDir, dir)
const config = await loadFixture(fixture, {
static: true,
generate: {
dir,
routes: [path]
}
})
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)
})
test(`payload hydration ${path}`, async () => {
const window = await renderAndGetWindow(url(path), {}, { loadedCallback: '_onNuxtLoaded', globals: { id: '__nuxt' } })
expect(window.__NUXT__.fetch).toEqual({ 0: { result: 'fetched' } })
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await server.close()
})
})
})

View File

@ -3,3 +3,14 @@
[pages/posts/_slug] [pages/posts/_slug]
</div> </div>
</template> </template>
<script>
export default {
data: () => ({
result: null
}),
fetch () {
this.result = process.server ? 'fetched' : null
}
}
</script>

View File

@ -1,6 +1,7 @@
import { resolve } from 'path' import { resolve } from 'path'
export default { export default {
target: 'static',
rootDir: __dirname, rootDir: __dirname,
srcDir: resolve(__dirname, '..'), srcDir: resolve(__dirname, '..'),
router: { router: {

View File

@ -1,6 +1,7 @@
import { resolve } from 'path' import { resolve } from 'path'
export default { export default {
target: 'static',
rootDir: __dirname, rootDir: __dirname,
srcDir: resolve(__dirname, '..'), srcDir: resolve(__dirname, '..'),
router: { router: {

View File

@ -1,6 +1,7 @@
import { resolve } from 'path' import { resolve } from 'path'
export default { export default {
target: 'static',
rootDir: __dirname, rootDir: __dirname,
srcDir: resolve(__dirname, '..'), srcDir: resolve(__dirname, '..'),
router: { router: {

View File

@ -12995,10 +12995,10 @@ ua-parser-js@^0.7.23:
resolved "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.23.tgz#704d67f951e13195fbcd3d78818577f5bc1d547b" resolved "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.23.tgz#704d67f951e13195fbcd3d78818577f5bc1d547b"
integrity sha512-m4hvMLxgGHXG3O3fQVAyyAQpZzDOvwnhOTjYz5Xmr7r/+LpkNy3vJXdVRWgd1TkAb7NGROZuSy96CrlNVjA7KA== integrity sha512-m4hvMLxgGHXG3O3fQVAyyAQpZzDOvwnhOTjYz5Xmr7r/+LpkNy3vJXdVRWgd1TkAb7NGROZuSy96CrlNVjA7KA==
ufo@^0.6.5: ufo@^0.6.6:
version "0.6.5" version "0.6.6"
resolved "https://registry.npmjs.org/ufo/-/ufo-0.6.5.tgz#90b14d516daf4cc66674f6a5ea5b32c6b257e3d2" resolved "https://registry.npmjs.org/ufo/-/ufo-0.6.6.tgz#6bc4b4de19ff9a9cb68134719190196b0cc7353b"
integrity sha512-QwmHDefpQSmULSXO0q6LPiaBBi7sDVYpunWaxs+P97I5PnK04IkcN5vp28Mk+b11FmAMgEhrGhuLuHdw8a0tEw== integrity sha512-HDhml2KskY1VVtQGckmaf/p3rTuleC2M7qL9Wf1dJZMw7glFk6oVjRsddf6LJp0I+pHERZzfttVmBMq1nR3PGA==
uglify-js@^3.1.4, uglify-js@^3.5.1: uglify-js@^3.1.4, uglify-js@^3.5.1:
version "3.12.7" version "3.12.7"