Skip to content

fix: Add is404 flag for 404ed routes#151

Merged
justlevine merged 37 commits into
rtCamp:developfrom
Swanand01:fix/status-code-for-404ed-routes
Apr 17, 2025
Merged

fix: Add is404 flag for 404ed routes#151
justlevine merged 37 commits into
rtCamp:developfrom
Swanand01:fix/status-code-for-404ed-routes

Conversation

@Swanand01

@Swanand01 Swanand01 commented Apr 9, 2025

Copy link
Copy Markdown
Contributor

What

This PR adds the is404 flag for 404ed routes

Why

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

parseQueryResult in packages/query/src/utils/parse-template.ts is refactored to also parse out the flag is404.

Testing Instructions

Screenshots

Additional Info

Checklist

  • I have read the Contribution Guidelines.
  • My code is tested to the best of my abilities.
  • My code passes all lints (ESLint, tsc, prettier etc.).
  • My code has detailed inline documentation.
  • I have added unit tests to verify the code works as intended.
  • I have updated the project documentation as needed.
  • I have added a changeset for this PR using npm run changeset.

@Swanand01 Swanand01 self-assigned this Apr 9, 2025
@changeset-bot

changeset-bot Bot commented Apr 9, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c97d860

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

This PR includes changesets to release 1 package
Name Type
@snapwp/query Patch

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

@Swanand01

Copy link
Copy Markdown
Contributor Author

@justlevine This here is a quick fix as recommended by @ayushnirwal:
#145 (comment)

@Swanand01 Swanand01 requested a review from ayushnirwal April 9, 2025 13:16
@coveralls

coveralls commented Apr 9, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14524438168

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 53.748%

Totals Coverage Status
Change from base Build 14477364702: 0.1%
Covered Lines: 400
Relevant Lines: 661

💛 - Coveralls

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread packages/query/src/utils/parse-template.ts Outdated
Comment thread packages/query/src/utils/parse-template.ts
@Swanand01

Copy link
Copy Markdown
Contributor Author

@ayushnirwal This PR for now just returns the is404 property from the parseQueryResult. However, it is still not accessible in the user land. Any ideas on how that can be achieved?

@Swanand01

Swanand01 commented Apr 10, 2025

Copy link
Copy Markdown
Contributor Author

In TemplateRenderer:

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.

@ayushnirwal

Copy link
Copy Markdown
Collaborator

We can redirect to next's 404 page if is404 is true. If next provides ways to attach status code we can do that.

@justlevine

Copy link
Copy Markdown
Collaborator

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.

@justlevine justlevine requested a review from Copilot April 10, 2025 11:32

Copilot AI 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.

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.
});

@ayushnirwal

ayushnirwal commented Apr 15, 2025

Copy link
Copy Markdown
Collaborator

@Swanand01 I don't know how you arrived at the middleware solution.

Try using the notFound function along with not-found.js file.

  1. Create a not-found file that fetches template data for WP 404 page. Since this fetch does not rely on any dynamic parameters, it will be statically built.

  2. In Template Renderer, call the function notFound.

@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.

@Swanand01 Swanand01 requested a review from justlevine April 15, 2025 08:08
@Swanand01

Copy link
Copy Markdown
Contributor Author

@SH4LIN I don't know how you arrived at the middleware solution.

Try using the notFound function along with not-found.js file.

  1. Create a not-found file that fetches template data for WP 404 page. Since this fetch does not rely on any dynamic parameters, it will be statically built.
  2. In Template Renderer, call the function notFound.

@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 TemplateRenderer in not-found.tsx?

export default function NotFound() {
	return (
		<TemplateRenderer>
			{ ( { editorBlocks } ) => {
				return <EditorBlocksRenderer editorBlocks={ editorBlocks } />;
			} }
		</TemplateRenderer>
	);
}

@ayushnirwal

Copy link
Copy Markdown
Collaborator

What if we use TemplateRenderer in not-found.tsx?

Yes...

@justlevine

Copy link
Copy Markdown
Collaborator

Is there a reliable URI to get the 404 template in (1) through templateByURI? or any other way?

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.

If no, we can use a garbage URI to fetch 404 template data for now and later update the backend to remove the hack.

I'm not opposed to this as a temporary workaround.

@justlevine justlevine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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. )

Comment thread packages/query/src/queries/get-current-template.graphql Outdated
Comment thread examples/nextjs/starter/src/app/[[...path]]/page.tsx Outdated
Comment thread packages/next/src/template-renderer/index.tsx Outdated
Comment thread .changeset/tame-beans-pay.md Outdated
@Swanand01 Swanand01 requested a review from justlevine April 17, 2025 06:46
@justlevine justlevine merged commit 693252c into rtCamp:develop Apr 17, 2025
justlevine added a commit that referenced this pull request Mar 12, 2026
* 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>
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.

6 participants