Skip to content

Commit 56f61c9

Browse files
committed
Added: support for HTTP status code (200 by default) and example of custom 404 error.
1 parent 8756103 commit 56f61c9

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/vue-ssr/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,60 @@ VueSSR.createApp = function (context) {
152152
}
153153
```
154154

155+
### Set status code for custom 404 error template
156+
157+
`NotFound.vue`
158+
159+
```vue
160+
<template>
161+
<h1>404</h1>
162+
</template>
163+
164+
<script>
165+
export default {
166+
name: 'NotFound',
167+
};
168+
</script>
169+
```
170+
171+
`routes.js`
172+
173+
```javascript
174+
import NotFound from '/imports/ui/views/NotFound';
175+
176+
export default [
177+
// ...
178+
{
179+
path: '*',
180+
name: 'not-found',
181+
component: NotFound,
182+
},
183+
];
184+
```
185+
186+
`vue-ssr.js`
187+
188+
```javascript
189+
VueSSR.createApp = function (context) {
190+
return new Promise((resolve, reject) => {
191+
const { app, router } = CreateApp()
192+
// Set the URL in the router
193+
router.push(context.url)
194+
195+
router.onReady(() => {
196+
// name of wildcard route
197+
if (router.currentRoute.name === 'not-found') {
198+
context.statusCode = 404;
199+
}
200+
201+
// ...
202+
203+
resolve(app)
204+
})
205+
})
206+
}
207+
```
208+
155209
---
156210

157211
LICENCE ISC - Created by Guillaume CHAU (@Akryum)

packages/vue-ssr/server/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ onPageLoad(sink => new Promise((resolve, reject) => {
126126
const head = ((appendHtml && appendHtml.head) || context.head) || ''
127127
const body = ((appendHtml && appendHtml.body) || context.body) || ''
128128
const js = ((appendHtml && appendHtml.js) || context.js) || ''
129+
const statusCode = ((appendHtml && appendHtml.statusCode) || context.statusCode) || 200
129130

130131
const script = js && `<script type="text/javascript">${js}</script>`
131132

133+
sink.setStatusCode(statusCode)
132134
sink.renderIntoElementById(VueSSR.outlet, html)
133135
sink.appendToHead(head)
134136
sink.appendToBody([body, script])

0 commit comments

Comments
 (0)