Skip to content

Commit

Permalink
Send status code 404 when router doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Sep 27, 2023
1 parent 03f92d2 commit e77dca6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
23 changes: 9 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,17 @@ export async function createServer(
render = (await import('./dist/server/entry-server.js')).render
}

const [appHtml, appState, preloadLinks, tagMeta, redirectRoute] =
const [appHtml, appState, preloadLinks, tagMeta, statusCode] =
await render(url, manifest, origin)
const html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-state-->`, appState)
.replace(`<!--head-tags-->`, tagMeta.headTags)
.replace(`<!--body-tags-open-->`, tagMeta.bodyTagsOpen)
.replace(`<!--body-tags-->`, tagMeta.bodyTags)
.replace(makeAppContainer(), makeAppContainer(appHtml))

if (redirectRoute) {
res.redirect(redirectRoute)
} else {
const html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-state-->`, appState)
.replace(`<!--head-tags-->`, tagMeta.headTags)
.replace(`<!--body-tags-open-->`, tagMeta.bodyTagsOpen)
.replace(`<!--body-tags-->`, tagMeta.bodyTags)
.replace(makeAppContainer(), makeAppContainer(appHtml))

res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
}
res.status(statusCode).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
Expand Down
7 changes: 3 additions & 4 deletions src/entry-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ export async function render(url, manifest, originUrl) {
// @vitejs/plugin-vue injects code into a component's setup() that registers
// itself on ctx.modules. After the render, ctx.modules would contain all the
// components that have been instantiated during this render call.

const ctx = {}
const initialRoute = router.currentRoute.value.path
const html = await renderToString(app, ctx)
const latestRoute = router.currentRoute.value.path
const redirectRoute = initialRoute !== latestRoute && latestRoute
const statusCode = router.currentRoute.value.meta?.statusCode || 200
const headPayload = await renderSSRHead(getActiveHead())
const renderState = `
<script>
Expand All @@ -37,7 +36,7 @@ export async function render(url, manifest, originUrl) {
// which we can then use to determine what files need to be preloaded for this
// request.
const preloadLinks = renderPreloadLinks(ctx.modules, manifest)
return [html, renderState, preloadLinks, headPayload, redirectRoute]
return [html, renderState, preloadLinks, headPayload, statusCode]
}

function renderPreloadLinks(modules, manifest) {
Expand Down
12 changes: 9 additions & 3 deletions src/modules/httpErrorPages/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ export default [
{
path: '/500',
name: 'httpError500',
component: internalError
component: internalError,
meta: {
statusCode: 500
}
},
{
path: '/:pathMatch(.*)*',
name: 'httpError400',
component: notFound
name: 'httpError404',
component: notFound,
meta: {
statusCode: 404
}
}
]
2 changes: 1 addition & 1 deletion src/modules/otus/views/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async function loadInitialData() {
function redirectOnError(error) {
switch (error?.response?.status) {
case 404:
router.replace({ name: 'httpError400' })
router.replace({ name: 'httpError404' })
break
case 500:
router.replace({ name: 'httpError500' })
Expand Down

0 comments on commit e77dca6

Please sign in to comment.