Skip to content

Add notFound() and NotFoundError to programmatically trigger 404 responses - #17967

Open
MGhafouriV wants to merge 1 commit into
withastro:mainfrom
MGhafouriV:feat/not-found-helper
Open

MGhafouriV wants to merge 1 commit into
withastro:mainfrom
MGhafouriV:feat/not-found-helper

Conversation

@MGhafouriV

Copy link
Copy Markdown

Changes

  • Adds notFound() and NotFoundError to virtual module astro:navigation to programmatically trigger 404 responses.
  • Allows external data loaders (e.g. loadBlog()), API routes, middleware, and .astro components to encapsulate 404 handling without repeating if (!item) return Astro.redirect('/404') checks across page frontmatters.
  • Intercepts NotFoundError in the SSR request pipeline (core/routing, core/pages, and core/middleware) so that calling notFound() gracefully renders 404.astro (with dynamic error props) while suppressing fatal 500 server crash logs.

Example

// src/lib/blog.ts
import { notFound } from 'astro:navigation';

export async function loadBlog(id: string) {
  const blog = await fetchBlog(id);
  if (!blog) {
    notFound('Blog post not found'); // Halts execution and triggers 404
  }
  return blog; // TypeScript automatically knows blog is defined!
}
---
// src/pages/blog/[id].astro
import { loadBlog } from '../../lib/blog';

// No need for repetitive `if (!blog) return Astro.redirect('/404')` checks
const blog = await loadBlog(Astro.params.id);
---
<h1>{blog.title}</h1>

Testing

  • Added integration tests in packages/astro/test/astro-not-found.test.ts covering .astro frontmatter, dynamic error props, API routes, middleware, standard 500 error propagation, and dev mode.
  • Added compile-time type tests in packages/astro/test/types/astro-navigation-virtual-module.ts verifying that notFound() returns never and NotFoundError has the expected prototype and properties.

Docs

  • Docs PR will be opened in withastro/docs to document the astro:navigation module and notFound() helper.

@changeset-bot

changeset-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f810374

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release labels Sep 10, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@codspeed-hq

codspeed-hq Bot commented Sep 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks


Comparing MGhafouriV:feat/not-found-helper (f810374) with main (750b4db)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (01395b8) during the generation of this report, so 750b4db was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@matthewp

Copy link
Copy Markdown
Contributor

This would make people think they can do this in components, but they cannot. Think it creates more misunderstandings than its worth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants