fix: Add is404 flag for 404ed routes#151
Conversation
chore: Add `import/no-default-export` eslint rule.
🦋 Changeset detectedLatest commit: c97d860 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
@justlevine This here is a quick fix as recommended by @ayushnirwal: |
Pull Request Test Coverage Report for Build 14524438168Details
💛 - Coveralls |
justlevine
left a comment
There was a problem hiding this comment.
LGTM with some small notes below.
That said, since this PR doesn't seem to actually do anything with / allow usage of is404 and since connectedNode is an unnecessary perf hit. I'm going to put this on hold.
If exposing template date or fixing the error codes gets unblocked before snapwp-helper can expose a templateByUri.is404, then we'll merge this as a temporary fix. Otherwise, we'll update this to use that eventual field.
|
@ayushnirwal This PR for now just returns the |
|
In const { editorBlocks, bodyClasses, stylesheets, scripts, scriptModules, is404 } =
await getTemplateData( pathname || '/' );
<main>
<div className="wp-site-blocks">
{ children( { editorBlocks, is404 } ) }
</div>
</main>export default function Page() {
return (
<TemplateRenderer>
{ ( { editorBlocks, is404 } ) => {
return <EditorBlocksRenderer editorBlocks={ editorBlocks } />;
} }
</TemplateRenderer>
);
}We can expose the is404 to user land this way. |
|
We can redirect to next's 404 page if is404 is true. If next provides ways to attach status code we can do that. |
The acceptance criteria for https://github.com/rtCamp/headless/issues/451 is to use WordPress as the source of truth for the 404 template while providing user control of status codes based on WPGraphQL-exposed data. I don't think a component-level redirect is an "ideal" DX solve for this, but we can refine it in a follow-up PR. |
There was a problem hiding this comment.
Copilot reviewed 6 out of 7 changed files in this pull request and generated no comments.
Files not reviewed (1)
- packages/query/src/queries/get-current-template.graphql: Language not supported
Comments suppressed due to low confidence (1)
packages/query/src/utils/tests/parse-template.test.ts:90
- There is no test case for when 'templateByUri' is undefined; consider adding one to ensure the 'is404' flag defaults to false in that scenario.
});
|
@Swanand01 I don't know how you arrived at the middleware solution. Try using the notFound function along with not-found.js file.
@justlevine Is there a reliable URI to get the 404 template in (1) through templateByURI? or any other way? If no, we can use a garbage URI to fetch 404 template data for now and later update the backend to remove the hack. |
What if we use export default function NotFound() {
return (
<TemplateRenderer>
{ ( { editorBlocks } ) => {
return <EditorBlocksRenderer editorBlocks={ editorBlocks } />;
} }
</TemplateRenderer>
);
} |
Yes... |
There's currently no way to fetch just a template and not a "resolved" URI. Ideally it would be the same results of the previous query - either via the app or because we have server-side caching. I know most things are locked down, but even just requerying the same URI will give us what we need.
I'm not opposed to this as a temporary workaround. |
justlevine
left a comment
There was a problem hiding this comment.
Remaining open comments + docs, and this should be good to merge.
(If we cant import getTemplateData() to userland until #144 , then we can doc the ideal dx and then put this on hold until that's possible. )
* feat : add import/no-default-export eslint rule. * chore : updated files for no-default-exports. * chore : add changeset. * chore : reorder-import * temp : fix failing gh actions * fix : export default for codegen config and baseConfig * feat: add connectedNode field to template query * feat: add `is404` flag to parseQueryResult return values * chore: add changeset * chore: add is404 test * feat: expose `is404` to the frontend * chore: use named import in next config * feat: add 404 handling to current path middleware * refactor: remove custom header * refactor: update is404 parsing * fix: update tests * revert: current path middleware * feat: redirect to not-found * revert: connectedNode from query * revert: not-found.tsx * revert: passing is404 to `TeemplateRenderer` children * feat: add docs on handling 404 pages * chore: update changeset * chore: sort query a to z * chore: format * chore: cleanup * docs: working example * chore: format --------- Co-authored-by: Ta5r <srv.tanay@gmail.com> Co-authored-by: Tanay Srivastava <62954323+Ta5r@users.noreply.github.com> Co-authored-by: Dovid Levine <david@axepress.dev>
What
This PR adds the
is404flag for 404ed routesWhy
Currently, when templateByUri{} resolves to a non-existent WordPress route, the 404 template gets returned, but since theres a template to return, the status code presents as a Success, instead of properly indicating (to SEO, crawlers, userland handlers) that there is no WP-route at that page.
Related Issue(s):
How
parseQueryResultinpackages/query/src/utils/parse-template.tsis refactored to also parse out the flagis404.Testing Instructions
Screenshots
Additional Info
Checklist
npm run changeset.