@@ -21,14 +21,73 @@ async function prerender() {
2121 const { render } = await import ( path . resolve ( dist , 'server/entry-server.js' ) )
2222
2323 for ( const route of routes ) {
24- const { html } = render ( route )
24+ const { html, helmet } = render ( route )
2525
26- // Inject pre-rendered content into the root div
27- const output = template . replace (
26+ // Start with injecting pre-rendered content into the root div
27+ let output = template . replace (
2828 '<div id="root"></div>' ,
2929 `<div id="root">${ html } </div>`
3030 )
3131
32+ // Inject page-specific <head> tags from react-helmet-async
33+ if ( helmet ) {
34+ const helmetTitle = helmet . title . toString ( )
35+ const helmetMeta = helmet . meta . toString ( )
36+ const helmetLink = helmet . link . toString ( )
37+ const helmetScript = helmet . script . toString ( )
38+
39+ // Replace the existing <title> with the page-specific one
40+ if ( helmetTitle ) {
41+ output = output . replace ( / < t i t l e > [ ^ < ] * < \/ t i t l e > / , helmetTitle )
42+ }
43+
44+ // Replace homepage meta description and robot-indexable meta tags with page-specific ones
45+ // Remove existing meta tags that helmet will provide (description, robots, og:*, twitter:*)
46+ if ( helmetMeta ) {
47+ output = output . replace (
48+ / \s * < m e t a n a m e = " d e s c r i p t i o n " [ ^ > ] * > / g,
49+ ''
50+ )
51+ output = output . replace (
52+ / \s * < m e t a n a m e = " r o b o t s " [ ^ > ] * > / g,
53+ ''
54+ )
55+ output = output . replace (
56+ / \s * < m e t a p r o p e r t y = " o g : [ ^ " ] * " [ ^ > ] * > / g,
57+ ''
58+ )
59+ output = output . replace (
60+ / \s * < ! - - O p e n G r a p h - - > / g,
61+ ''
62+ )
63+ output = output . replace (
64+ / \s * < m e t a n a m e = " t w i t t e r : [ ^ " ] * " [ ^ > ] * > / g,
65+ ''
66+ )
67+ output = output . replace (
68+ / \s * < ! - - T w i t t e r C a r d - - > / g,
69+ ''
70+ )
71+ // Inject helmet meta tags before </head>
72+ output = output . replace ( '</head>' , ` ${ helmetMeta } \n </head>` )
73+ }
74+
75+ // Replace homepage canonical link with page-specific one
76+ if ( helmetLink ) {
77+ output = output . replace (
78+ / \s * < l i n k r e l = " c a n o n i c a l " [ ^ > ] * > / g,
79+ ''
80+ )
81+ // Inject helmet link tags before </head>
82+ output = output . replace ( '</head>' , ` ${ helmetLink } \n </head>` )
83+ }
84+
85+ // Inject any page-specific script tags (e.g., JSON-LD structured data)
86+ if ( helmetScript ) {
87+ output = output . replace ( '</head>' , ` ${ helmetScript } \n </head>` )
88+ }
89+ }
90+
3291 // Determine output path
3392 const outputPath =
3493 route === '/'
0 commit comments