feat(router): ev.internalRequest information#8698
Conversation
🦋 Changeset detectedLatest commit: 27505d5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
@qwik.dev/core
@qwik.dev/router
eslint-plugin-qwik
create-qwik
@qwik.dev/optimizer
commit: |
There was a problem hiding this comment.
Pull request overview
This PR improves Qwik Router’s handling of internal JSON requests (q-loader and fetch-based qaction), ensuring redirects are surfaced to the client as loader/action-compatible JSON signals and giving middleware better context to avoid accidentally rewriting internal requests.
Changes:
- Convert unfollowed HTTP 3xx responses from loader fetches into
{ r: location }loader redirect envelopes. - Route q-loader requests through the page’s middleware chain by validating and honoring
X-Qwik-fullpath. - Expose
RequestEvent.internalRequest(false | 'loader' | 'action') across runtime types, middleware types, and documentation.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/qwik-router/src/runtime/src/use-endpoint.unit.ts | Adds a regression test asserting 3xx loader fetches are converted into loader redirect envelopes. |
| packages/qwik-router/src/runtime/src/types.ts | Re-exports the new InternalRequest type from middleware for runtime consumers. |
| packages/qwik-router/src/runtime/src/route-loaders.ts | Adds 3xx/Location handling in fetchRouteLoaderData() for unfollowed redirects. |
| packages/qwik-router/src/runtime/src/qwik-router.runtime.api.md | Updates the API report to include InternalRequest in the runtime surface. |
| packages/qwik-router/src/runtime/src/index.ts | Exports InternalRequest from the runtime entrypoint. |
| packages/qwik-router/src/middleware/request-handler/types.ts | Introduces InternalRequest and adds internalRequest to RequestEventBase. |
| packages/qwik-router/src/middleware/request-handler/request-path.unit.ts | Adds tests for validating internal full pathnames used by q-loader routing. |
| packages/qwik-router/src/middleware/request-handler/request-path.ts | Adds resolveValidInternalFullPathname() helper for safe X-Qwik-fullpath usage. |
| packages/qwik-router/src/middleware/request-handler/request-handler.unit.ts | Tests getRequestHandlerPathname() behavior for q-loader vs normal page requests. |
| packages/qwik-router/src/middleware/request-handler/request-handler.ts | Routes q-loader requests based on validated X-Qwik-fullpath (when applicable). |
| packages/qwik-router/src/middleware/request-handler/request-event.unit.ts | Adds unit tests for RequestEvent.internalRequest detection semantics. |
| packages/qwik-router/src/middleware/request-handler/request-event-core.ts | Implements internalRequest getter on the request event instance. |
| packages/qwik-router/src/middleware/request-handler/middleware.request-handler.api.md | Updates the middleware API report with InternalRequest and internalRequest. |
| packages/qwik-router/src/middleware/request-handler/index.ts | Re-exports InternalRequest from the middleware entrypoint. |
| packages/qwik-router/src/middleware/request-handler/handlers/json-request-wrapper.unit.ts | Tests that loader middleware redirects are serialized into loader JSON redirect envelopes. |
| packages/qwik-router/src/middleware/request-handler/handlers/json-request-wrapper.ts | Uses shared validation helper and serializes loader redirects into JSON envelopes. |
| packages/docs/src/routes/docs/(qwikrouter)/routing/index.mdx | Documents internalRequest on RequestEvent. |
| packages/docs/src/routes/docs/(qwikrouter)/middleware/index.mdx | Adds a dedicated internalRequest documentation section. |
| packages/docs/src/routes/docs/(qwikrouter)/guides/rewrites/index.mdx | Updates rewrite guidance to guard against rewriting internal JSON requests. |
| packages/docs/src/routes/docs/(qwikrouter)/advanced/request-handling/index.mdx | Adds internalRequest to the request event overview table. |
| packages/docs/src/routes/api/qwik-router-middleware-request-handler/index.mdx | Updates generated API docs for InternalRequest and RequestEventBase.internalRequest. |
| packages/docs/src/routes/api/qwik-router-middleware-request-handler/api.json | Updates generated API JSON to include InternalRequest and internalRequest. |
| .changeset/router-loader-redirects.md | Adds a patch changeset for q-loader middleware chain + unfollowed redirect handling. |
| .changeset/router-internal-request.md | Adds a patch changeset for exposing RequestEvent.internalRequest. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
built with Refined Cloudflare Pages Action⚡ Cloudflare Pages Deployment
|
| import type { RequestHandler } from '@qwik.dev/router'; | ||
|
|
||
| export const onRequest: RequestHandler = async ({ url, rewrite, internalRequest }) => { | ||
| if (internalRequest) { |
There was a problem hiding this comment.
Are there really no better alternatives? This is not so great DX wise since devs must know about it... Ideally redirects should just work without having to remember to check for internal requests.
There was a problem hiding this comment.
that's impossible:
Either middleware doesn't run for loader requests, and it will miss logged-in state etc,
or the dev always redirects even for loaders, which breaks things.
This is because now each loader gets its own request, which allows sane loader caching.
In any case, no matter which API we would use, we still need to educate the dev. Having a prop directly visible in the type on requestEv is the most direct thing we can do.
f0c64b3 to
27505d5
Compare
No description provided.