Repository navigation
fix(field): use the content type's record route in the snippet preview - #64
Merged
Merged
Conversation
The Google snippet preview in the editor always built its URL from the built-in `record` route. For a content type with a custom `record_route`, or a localized record edited in a non-default locale, the preview showed a URL the page is not served at. Build it with Bolt's canonical() Twig function from the content type's `record_route` and the edited locale, the same way Bolt generates record links. canonical() also drops parameters the route doesn't need and the default locale (per `remove_default_locale_on_canonical`), so the preview is unchanged for content types using the default routes. Refs AppoloDev#18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #18
Summary
#18 reports that the extension doesn't work for content types with a custom
record_route(e.g.record_route: content_seo).The frontend part is already fixed on
main.Seono longer switches on therecordroute name. Any route that exposes arecordresolves the title, description and other values from the record's SEO field, andoverride_defaultcan be keyed by the custom route name. I verified this with a customcontent_seoroute.What remained is the Google snippet preview in the editor. Its URL was always built from the built-in
recordroute:absolute_url(path('record', { 'contentTypeSlug': ..., 'slugOrId': 'REPLACE' }))so the preview showed a URL the page isn't served at when the content type:
record_route, orChange
The preview URL is now built with Bolt's
canonical()Twig function from the content type'srecord_routeand the locale being edited (currentlocale). Bolt generates record links the same way (ContentHelper::getLink()).canonical()also:contentTypeSlugwhen a custom route doesn't need it (no stray?contentTypeSlug=…),localization.remove_default_locale_on_canonical,general/canonicalhost, matching the URL on the live page.Backwards compatibility
This is a template-only change, with no PHP or config changes. For content types using the default
record/record_localeroutes, the preview URL is the same as before when editing the default locale. Because the preview now uses the configuredgeneral/canonicalhost instead of the backend request's host, it shows the public URL even when the admin is reached through a different hostname.Manual verification (Bolt 6.1.9)
I tested this end-to-end on a fresh
bolt/projectinstall with the demo fixtures, plus three setups:pages(defaultrecordroute),entriesmade localized (en,nl, sorecord_locale), andproductswithrecord_route: product_seomapped to/shop/{slugOrId}. For each record, I took the snippet preview URL from the real edit page, substituted the slug the wayseo.jsdoes, and fetched that URL on the frontend.<link rel="canonical">pages(record)/page/this-is-a-page/page/this-is-a-page/page/this-is-a-pageentries(record_locale), editingen/entry/…/entry/…/entry/…entries(record_locale), editingnl/entry/…❌/nl/entry/…/nl/entry/…products(customproduct_seo)/product/…❌/shop/…/shop/…After the change, every preview URL returns HTTP 200 and is identical to the page's canonical URL.
With
remove_default_locale_on_canonical: false, Bolt itself renders the canonical of a non-localized record as…/page/this-is-a-page?_locale=en, becauseContentHelperalways passes_locale. The preview now shows that same URL, which is the one search engines are told to index. Before, it showed the bare/page/this-is-a-page.Tests
New
tests/Twig/SeoFieldTemplateTest.phprenders the snippet preview block through a real Twig environment, with Bolt's functions stubbed:testSnippetUrlUsesTheContentTypeRecordRoute: a customrecord_routeis used.testSnippetUrlUsesTheEditedLocale: the edited locale is passed.Both fail without the change. The suite passes (114 tests), and PHPStan is clean.