Skip to content
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

way to disable 404 route #1918

Open
harrysolovay opened this issue Oct 12, 2023 · 4 comments · May be fixed by #2030
Open

way to disable 404 route #1918

harrysolovay opened this issue Oct 12, 2023 · 4 comments · May be fixed by #2030

Comments

@harrysolovay
Copy link

Follow-up from #1916

Might it be valuable to provide a way to disable the 404 route?

@lionel-rowe
Copy link
Contributor

FWIW, you can work around it fairly easily by checking the status of the Response object returned from freshHandler:

const freshHandler = await createHandler(manifest, config);

Deno.serve(async (req, info) => {
  const { pathname } = new URL(req.url);

  if (pathname === "/special-route") {
    return new Response('special route');
  }

  const res = await freshHandler(req, info);

  if (res.status === 404) {
    return new Response('overridden 404 response', { status: 404 });
  }

  return res;
});

Only drawback is that the Fresh 404 page is still rendered (i.e. slight performance hit, and any side effects from rendering it will still happen).

@harrysolovay
Copy link
Author

Good idea! If the 404 page is empty, this shouldn't be too bad. Still, a way to opt out would be imo valuable.

@deer
Copy link
Contributor

deer commented Nov 1, 2023

@harrysolovay @lionel-rowe, what's the expected behavior here? I've modified RouterOptions so it looks like this:

export interface RouterOptions {
  trailingSlash?: boolean;
  ignoreFilePattern?: RegExp;
  disable404?: boolean;
}

So you can now use this (at least in my branch on my computer) to disable the 404 page. But what does that mean exactly? Right now I have this diff somewhere within the server code:

               async renderNotFound<Data = undefined>(data: Data) {
+                if (disable404) {
+                  throw new Deno.errors.NotFound();
+                }
                 return await renderNotFound(req, params, ctx, data);
               },

So if the flag is true, instead of running the renderNotFound handler, it will throw a not found error. Is this what you're looking for? I guess that error can then be caught and dealt with on your own.

@harrysolovay
Copy link
Author

Is this what you're looking for?

Yes.

I guess that error can then be caught and dealt with on your own.

This would be a fine solution, although returning undefined may be preferable imo.

@deer deer linked a pull request Nov 8, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants