11import type { SeoConfig } from "@shopify/hydrogen" ;
2- import type { RouteLoaderArgs } from "@weaverse/hydrogen" ;
3- import type { MetaFunction } from "react-router" ;
2+ import type { LoaderFunctionArgs , MetaFunction } from "react-router" ;
43import { redirect } from "react-router" ;
54import type { ArticleQuery } from "storefront-api.generated" ;
65import invariant from "tiny-invariant" ;
6+ import { loadArticlePage } from "~/utils/article-page.server" ;
7+ import { resolveArticleBlog } from "~/utils/blog.server" ;
8+ import { articlePath , blogPath , blogSeoMeta } from "~/utils/blog-path" ;
79import { routeHeaders } from "~/utils/cache" ;
8- import { DEFAULT_BLOG_HANDLE } from "~/utils/const" ;
9- import { redirectIfHandleIsLocalized } from "~/utils/redirect" ;
1010import { skipPageRevalidationForStorefrontActions } from "~/utils/revalidation" ;
1111import { seoPayload } from "~/utils/seo.server" ;
1212import { localizedSeoMeta } from "~/utils/seo-translation" ;
@@ -15,41 +15,65 @@ import { WeaverseContent } from "~/weaverse";
1515export const headers = routeHeaders ;
1616export const shouldRevalidate = skipPageRevalidationForStorefrontActions ;
1717
18- export async function loader ( args : RouteLoaderArgs ) {
18+ export async function loader ( args : LoaderFunctionArgs ) {
1919 const { request, params, context } = args ;
2020 const { storefront } = context . weaverse ;
2121 const { language, country } = storefront . i18n ;
2222
2323 invariant ( params . articleHandle , "Missing article handle" ) ;
2424
2525 const { articleHandle } = params ;
26- if ( articleHandle === DEFAULT_BLOG_HANDLE ) {
27- const localePrefix = params . locale ? `/${ params . locale } ` : "" ;
28- return redirect ( `${ localePrefix } /blogs` , 301 ) ;
26+ const url = new URL ( request . url ) ;
27+ const requestedBlog = params . blogHandle ?? url . searchParams . get ( "blog" ) ;
28+ const blogHandle = await resolveArticleBlog (
29+ storefront ,
30+ articleHandle ,
31+ requestedBlog ,
32+ ) ;
33+ if ( ! blogHandle ) {
34+ // Shopify's /blogs/:blogHandle listing aliases share the short article path.
35+ const { blog : listing } = await storefront . query ( BLOG_ALIAS_QUERY , {
36+ variables : { handle : articleHandle , language } ,
37+ } ) ;
38+ if ( ! requestedBlog && listing ) {
39+ const destination = new URL ( blogPath ( listing . handle ) , url ) ;
40+ url . searchParams . set ( "blog" , listing . handle ) ;
41+ return redirect (
42+ `${ params . locale ? `/${ params . locale } ` : "" } ${ destination . pathname } ?${ url . searchParams } ` ,
43+ 301 ,
44+ ) ;
45+ }
46+ throw new Response ( null , { status : 404 } ) ;
2947 }
3048
3149 const [ { blog } , weaverseData ] = await Promise . all ( [
3250 storefront . query < ArticleQuery > ( ARTICLE_QUERY , {
3351 variables : {
34- blogHandle : DEFAULT_BLOG_HANDLE ,
52+ blogHandle,
3553 articleHandle,
3654 language,
3755 } ,
3856 } ) ,
39- context . weaverse . loadPage ( {
40- type : "ARTICLE" ,
41- handle : articleHandle ,
57+ loadArticlePage ( context , {
58+ blogHandle,
59+ articleHandle,
60+ locale : params . locale ,
4261 } ) ,
4362 ] ) ;
4463
4564 if ( ! blog ?. articleByHandle ) {
4665 throw new Response ( null , { status : 404 } ) ;
4766 }
4867
49- redirectIfHandleIsLocalized ( request , {
50- handle : articleHandle ,
51- data : blog . articleByHandle ,
52- } ) ;
68+ if ( articleHandle !== blog . articleByHandle . handle ) {
69+ // Replace the article segment only: its text may also occur in the locale
70+ // or parent blog. Keep Studio's full path and the request's query intact.
71+ const segments = url . pathname . split ( "/" ) ;
72+ const articleIndex = segments . length - ( url . pathname . endsWith ( "/" ) ? 2 : 1 ) ;
73+ segments [ articleIndex ] = encodeURIComponent ( blog . articleByHandle . handle ) ;
74+ url . pathname = segments . join ( "/" ) ;
75+ throw redirect ( url . toString ( ) ) ;
76+ }
5377
5478 const article = blog . articleByHandle ;
5579 const relatedArticles = blog . articles . nodes . filter (
@@ -62,12 +86,18 @@ export async function loader(args: RouteLoaderArgs) {
6286 day : "numeric" ,
6387 } ) . format ( new Date ( article . publishedAt ) ) ;
6488
65- const seo = seoPayload . article ( { article, url : request . url } ) ;
89+ // Include the parent in canonical/share URLs, including legacy short requests.
90+ const articleUrl = new URL (
91+ articlePath ( article . handle , blog . handle ) ,
92+ request . url ,
93+ ) ;
94+ articleUrl . pathname = `${ params . locale ? `/${ params . locale } ` : "" } ${ articleUrl . pathname } ` ;
95+ const seo = seoPayload . article ( { article, url : articleUrl . toString ( ) } ) ;
6696
6797 return {
6898 article,
6999 blog : {
70- handle : DEFAULT_BLOG_HANDLE ,
100+ handle : blog . handle ,
71101 } ,
72102 relatedArticles,
73103 formattedDate,
@@ -77,7 +107,10 @@ export async function loader(args: RouteLoaderArgs) {
77107}
78108
79109export const meta : MetaFunction < typeof loader > = ( { data, matches } ) => {
80- return localizedSeoMeta ( matches , data ?. seo as SeoConfig ) ;
110+ return blogSeoMeta (
111+ localizedSeoMeta ( matches , data ?. seo as SeoConfig ) ,
112+ data ?. seo ?. url ,
113+ ) ;
81114} ;
82115
83116export default function Article ( ) {
@@ -122,6 +155,7 @@ const ARTICLE_QUERY = `#graphql
122155 }
123156 }
124157 fragment Article on Article {
158+ blog { handle }
125159 tags
126160 author: authorV2 {
127161 name
@@ -142,3 +176,9 @@ const ARTICLE_QUERY = `#graphql
142176 title
143177 }
144178` as const ;
179+
180+ const BLOG_ALIAS_QUERY = `#graphql
181+ query MerchantBlogAlias($handle: String!, $language: LanguageCode) @inContext(language: $language) {
182+ blog(handle: $handle) { handle }
183+ }
184+ ` as const ;
0 commit comments