Skip to content

Added support for: reject status code and message; context.statusCode. #397

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion packages/vue-ssr/README.md
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ VueSSR.createApp = function (context) {

// no matched routes
if (!matchedComponents.length) {
reject({ code: 404 })
reject({ code: 404, message: 'Not found' })
}

// Can use components prefetch here...
@@ -152,6 +152,60 @@ VueSSR.createApp = function (context) {
}
```

### Set status code for custom 404 error template

`NotFound.vue`

```vue
<template>
<h1>404</h1>
</template>

<script>
export default {
name: 'NotFound',
};
</script>
```

`routes.js`

```javascript
import NotFound from '/imports/ui/views/NotFound';

export default [
// ...
{
path: '*',
name: 'not-found',
component: NotFound,
},
];
```

`vue-ssr.js`

```javascript
VueSSR.createApp = function (context) {
return new Promise((resolve, reject) => {
const { app, router } = CreateApp()
// Set the URL in the router
router.push(context.url)

router.onReady(() => {
// name of wildcard route
if (router.currentRoute.name === 'not-found') {
context.statusCode = 404;
}

// ...

resolve(app)
})
})
}
```

---

LICENCE ISC - Created by Guillaume CHAU (@Akryum)
9 changes: 6 additions & 3 deletions packages/vue-ssr/server/index.js
Original file line number Diff line number Diff line change
@@ -64,8 +64,9 @@ patchSubscribeData(VueSSR)

const renderer = createRenderer()

function writeServerError (sink) {
sink.appendToBody('Server Error')
function writeServerError (sink, { code = 500, message = 'Server Error' } = {}) {
sink.setStatusCode(code)
sink.appendToBody(message)
}

WebApp.rawConnectHandlers.use(cookieParser())
@@ -125,9 +126,11 @@ onPageLoad(sink => new Promise((resolve, reject) => {
const head = ((appendHtml && appendHtml.head) || context.head) || ''
const body = ((appendHtml && appendHtml.body) || context.body) || ''
const js = ((appendHtml && appendHtml.js) || context.js) || ''
const statusCode = ((appendHtml && appendHtml.statusCode) || context.statusCode) || 200

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

sink.setStatusCode(statusCode)
sink.renderIntoElementById(VueSSR.outlet, html)
sink.appendToHead(head)
sink.appendToBody([body, script])
@@ -137,7 +140,7 @@ onPageLoad(sink => new Promise((resolve, reject) => {
)
}).catch(e => {
console.error(e)
writeServerError(sink)
writeServerError(sink, e)
resolve()
})
} catch (error) {