Skip to content

fix(ssg): pass SSG_CONTEXT to forGetInfoURLRequest#4810

Merged
yusukebe merged 2 commits intohonojs:mainfrom
yuintei:fix/ssg-pass-ssg-context
Mar 23, 2026
Merged

fix(ssg): pass SSG_CONTEXT to forGetInfoURLRequest#4810
yusukebe merged 2 commits intohonojs:mainfrom
yuintei:fix/ssg-pass-ssg-context

Conversation

@yuintei
Copy link
Copy Markdown
Contributor

@yuintei yuintei commented Mar 19, 2026

fix(ssg): pass SSG_CONTEXT to forGetInfoURLRequest

Summary

Pass SSG_CONTEXT to forGetInfoURLRequest fetch (in fetchRoutesContent) so that isSSGContext(c) returns true and disableSSG() works as intended.

await pool.run(() => app.fetch(forGetInfoURLRequest, { [SSG_CONTEXT]: true })) // Pass SSG_CONTEXT

Details

In fetchRoutesContent, SSG_CONTEXT is missing in the forGetInfoURLRequest fetch, while it is passed correctly in the replacedUrlParam fetch.

As a result, during the build process,

  • isSSGContext(c) incorrectly returns false.
  • disableSSG() falls back to next() instead of c.notFound().
  • Execution leaks into middlewares/handlers.
  • In Cloudflare Workers projects, c.env is undefined during the Vite build, causing the following errors in every route:
TypeError: Cannot read properties of undefined (reading 'ADMIN_PASSWORD')
TypeError: Cannot read properties of undefined (reading 'd1')

I had to add manual guards like if (!c.env) return next() or if (!c.env) return c.notFound() to every app.use() and app.get() that accesses c.env, which is what disableSSG() should be handling automatically.

With this fix, I verified that the errors above are resolved and the manual guards are no longer needed. All existing tests pass with this change.

To reproduce,

  1. Add a middleware that reads c.env (e.g. basicAuth with c.env.ADMIN_PASSWORD)
  2. Add a route handler with disableSSG() that accesses c.env bindings (e.g. c.env.d1)
  3. Run vite build with @hono/vite-ssg or toSSG

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.82%. Comparing base (fe689ec) to head (a2c2d73).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4810      +/-   ##
==========================================
- Coverage   92.83%   92.82%   -0.01%     
==========================================
  Files         177      177              
  Lines       11642    11642              
  Branches     3466     3466              
==========================================
- Hits        10808    10807       -1     
- Misses        833      834       +1     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Copy Markdown
Member

@yuintei Thanks!

@yusukebe yusukebe merged commit be85106 into honojs:main Mar 23, 2026
20 checks passed
@yuintei yuintei deleted the fix/ssg-pass-ssg-context branch March 23, 2026 12:48
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 this pull request may close these issues.

2 participants