question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide way of forcing a 404

See original GitHub issue

Let’s say you have a page file that matches the current route, but upon fetching some data in the addPageContext hook, you determine that necessary data is unavailable and the user should be served with a 404 error page. At present, I believe the only option available to a user here is to halt rendering of the page and render an error would be to throw an error within the addPageContext hook. However, this will end up as a 500, not a 404. In this specific case, we are deliberately throwing an error that we would like to be cast as a 404.

I have a suggestion for how this could be achieved - export error classes that can be imported and used to throw specific error types. E.g.


import { ServerError, NotFoundError } from 'vite-plugin-ssr';

export async function addPageContext({ url }) {

  const criticalData = await fetchCriticalData(url);

  if (!criticalData) {
    throw new NotFoundError;
  }
  return {
    criticalData
  }
}


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
brilloutcommented, Jun 25, 2021

What is your concrete use case?

You can already hack something like this today.

export async function render() {
  if (!criticalData) {
    return {dataMissing: true};
  }
}
// server.js

const renderPage = createPageRender(/*...*/)

app.get('*', async (req, res, next) => {
  const result = await renderPage({ url: req.url })
  if (result.dataMissing) {
    const result = await renderPage({ url: '/doesNotExist' })
    res.status(result.statusCode).send(result.renderResult)
    return;
  }
})

return render404Error(Page)

Yea I was thinking about something like this as well.

0reactions
brilloutcommented, Oct 20, 2021

I still struggle to see a use case for this. But I’m happy to be shown wrong and more than happy to re-open this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is a 404 Error? How to Deal With the Web Error
A 404 error indicates that the webpage you're trying to reach can't be found, and usually means that the page has moved or...
Read more >
How to Fix Error 404 Not Found on Your WordPress Site - Kinsta
The easiest way to fix this is to update your permalink settings through the WordPress dashboard. All you need to do is go...
Read more >
Apache: forcing a 404 status code - tomcat - Stack Overflow
I need to figure out a way to make these return a 404 so that these URLs drop out of the search engine...
Read more >
How to Create a Spectacular 404 Error Page (with 12 Examples)
3 methods to create a customized error 404 page · 1. Use 404 Page WordPress Plugin – You are lucky if you run...
Read more >
Error 404 not found - What does it mean & how to fix it! - IONOS
Reload the page: It might be that the error 404 has appeared for the simple reason that the page did not load properly....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found