Skip to content

c.notFound() in a page loader renders a static file at the same path as page props / 页面 loader 调用 c.notFound() 时,同路径的静态文件被当作页面 props 渲染 #776

Closed

Description

@wintercn

Problem

When a page loader calls c.notFound() and a static file exists at the same path, the static file is rendered as the page props instead of a 404 page.

Versions: void@1.7.0, @void/vue@1.7.0.

Reproduction

https://gist.github.com/wintercn/c91780104141a616d9f13bbed21b474a

The page loader of /docs/:name returns { doc: { title: 'Intro' } } for intro and calls c.notFound() otherwise. public/docs/readme.json contains { "static": "docs" }. Run npm run build and npm run preview.

Fresh preview process, request in this order:

/docs/intro            200 text/html         <h1>Intro</h1>
/docs/readme.json      200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')
/docs/intro            200 text/html         <h1>Intro</h1>

Fresh preview process, request in this order:

/docs/readme.json      200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')
/docs/intro            200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')

In the second process, every later /docs/intro stays empty because of #773.

Expected

/docs/readme.json returns the 404 page.

Analysis

  • createPagesFallback is registered as the global app.notFound (void/dist/index.mjs:2606). Its first step is assets.fetch(c.req.raw) (void/dist/runtime/routing.mjs:221), and it returns the asset when the status is not 404.
  • Hono calls this global handler for c.notFound() in a page loader, although the route has already matched.
  • runLoader (void/dist/protocol-yxdHxFLN.mjs:170) treats a 200 application/json Response as page props, so the static file content is rendered as props.

A possible fix: when notFound() is called in a page context, render the 404 page without looking up static assets.


问题

页面 loader 调用 c.notFound() 时,如果同一路径存在静态文件,这个静态文件会被当作页面 props 渲染,而不是返回 404 页面。

版本:void@1.7.0、@void/vue@1.7.0。

复现

https://gist.github.com/wintercn/c91780104141a616d9f13bbed21b474a

/docs/:name 的页面 loader 对 intro 返回 { doc: { title: 'Intro' } },其他情况调用 c.notFound()。public/docs/readme.json 的内容是 { "static": "docs" }。执行 npm run build 和 npm run preview。

新预览进程,依次请求:

/docs/intro            200 text/html         <h1>Intro</h1>
/docs/readme.json      200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')
/docs/intro            200 text/html         <h1>Intro</h1>

新预览进程,依次请求:

/docs/readme.json      200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')
/docs/intro            200 text/html         empty page, log: TypeError: Cannot read properties of undefined (reading 'title')

第二个进程中,由于 #773,之后的 /docs/intro 一直是空页面。

预期

/docs/readme.json 返回 404 页面。

分析

  • createPagesFallback 被注册为全局的 app.notFound(void/dist/index.mjs:2606),第一步就是 assets.fetch(c.req.raw)(void/dist/runtime/routing.mjs:221),状态不是 404 就直接返回这个文件。
  • 页面 loader 里的 c.notFound() 会调用这个全局处理器,而此时路由已经匹配。
  • runLoader(void/dist/protocol-yxdHxFLN.mjs:170)把 200 的 application/json Response 当作页面 props,静态文件的内容因此被当作 props 渲染。

可行的修法:在页面上下文中调用 notFound() 时,直接输出 404 页面,不查静态资源。

Activity

  1. 0froq commented on Nov 13, 2026

    @0froq
    Collaborator

    I can reproduce this on main. The loader returns the fallback's response, because Hono resolves c.notFound() inside the call, and a 200 JSON static file at that path is then read as page props. I'll open a PR shortly.

  2. self-assigned this
    on Nov 13, 2026
  3. added a commit that references this issue on Nov 14, 2026
    5973629
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions