Skip to content

Commit

Permalink
fix: request/response missing in useSSRContext, add response.cookie f…
Browse files Browse the repository at this point in the history
…unction in dev
  • Loading branch information
yooouuri committed Dec 20, 2023
1 parent 4f3427f commit e9d6201
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@nuxt/devalue": "^2.0.2",
"@types/express": "^4.17.21",
"cheerio": "1.0.0-rc.12",
"cookie": "^0.6.0",
"cookie-parser": "^1.4.6"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import type { App } from 'vue'
import { renderToString, SSRContext } from 'vue/server-renderer'
// @ts-ignore
import cookieParser from 'cookie-parser'
import type { Request, Response } from 'express'
// @ts-ignore
import * as cookie from 'cookie'
import { transformEntrypoint } from './transformEntrypoint'
import { generateHtml } from './generateHtml'
import type { Params, CallbackFn } from '../types'
Expand Down Expand Up @@ -75,8 +78,8 @@ export default function vueSsrPlugin(): Plugin {
if (ssr) {
return () => {
server.middlewares.use(cookieParser())
server.middlewares.use(async (req, res) => {
const url = req.originalUrl
server.middlewares.use(async (request, response) => {
const url = request.originalUrl

let template: string | undefined = readFileSync(resolve(cwd(), 'index.html'), 'utf-8')
template = await server.transformIndexHtml(url!, template)
Expand All @@ -88,18 +91,26 @@ export default function vueSsrPlugin(): Plugin {
const { app, router, state, head } = vueSSR(App, { routes, scrollBehavior }, undefined, true, true)

if (cb !== undefined) {
cb({ app, router, state })
// cb({ app, router, state, req, res })
// @ts-ignore
cb({ app, router, state, request, response })
}

await router.push(url!)
await router.isReady()

let redirect = null

const cookies = new Set<string>()

const ctx: SSRContext = {
req,
res,
request,
response: {
// https://github.com/expressjs/express/blob/master/lib/response.js#L854-L887
cookie: (name: string, value: string, options: any) => {
cookies.add(cookie.serialize(name, value, options))
},
...response,
},
redirect: (url: string) => {
redirect = url
},
Expand All @@ -117,16 +128,18 @@ export default function vueSsrPlugin(): Plugin {
head,
loadedModules)

response.setHeader('Set-Cookie', [...cookies])

if (redirect !== null) {
// https://github.com/vitejs/vite/discussions/6562#discussioncomment-1999566
res.writeHead(302, {
response.writeHead(302, {
location: redirect,
})
res.end()
}).end()

return
}

res.end(html)
response.end(html)
})
}
}
Expand All @@ -147,8 +160,7 @@ async function generateTemplate(
const { app, router, state, head } = vueSSR(App, { routes, scrollBehavior }, undefined, true, true)

if (cb !== undefined) {
cb({ app, router, state })
// cb({ app, router, state, req, res })
cb({ app, router, state, request, response })
}

await router.push(url!)
Expand All @@ -157,8 +169,8 @@ async function generateTemplate(
let redirect = null

const ctx: SSRContext = {
req: request,
res: response,
request,
response,
redirect: (url: string) => {
redirect = url
},
Expand Down

0 comments on commit e9d6201

Please sign in to comment.