PulseCanvas / website · pulsecanvas.ai / scripts / seo.mjs
| 1 | import { websiteUrl, seoPages, socialImage } from '../seo.config.js'; |
| 2 | const absolute = (path) => new URL(path, websiteUrl).href; |
| 3 | // NO SEO (2026-12-02): no sitemap, llms.txt or crawler courting. robots.txt just allows everyone. |
| 4 | export const robots = 'User-agent: *\nAllow: /\n'; |
| 5 | export function seoPlugin() { |
| 6 | return { |
| 7 | name: 'pulsecanvas-seo', |
| 8 | transformIndexHtml: { |
| 9 | order: 'pre', |
| 10 | handler(html, context) { |
| 11 | const path = context.path.replace(/index\.html$/, '').replace(/\/{2,}/g, '/'); |
| 12 | if (path === "/404.html") return html; |
| 13 | const page = seoPages[path]; |
| 14 | if (!page) throw new Error(`Missing page metadata for ${context.path}`); |
| 15 | // Replace legacy metadata, so every route has exactly one canonical title and description. |
| 16 | html = html.replace('href="/images/app-icon.png"', 'href="/images/favicon.png"') |
| 17 | .replace(/<title>[\s\S]*?<\/title>/gi, '') |
| 18 | .replace(/<meta\b(?=[^>]*\b(?:name\s*=\s*["'](?:description|robots|msvalidate\.01|twitter:[^"']+)["']|property\s*=\s*["']og:[^"']+["']))[^>]*>/gi, '') |
| 19 | .replace(/<link\b(?=[^>]*\brel\s*=\s*["']canonical["'])[^>]*>/gi, '') |
| 20 | .replace(/<script\b(?=[^>]*\btype\s*=\s*["']application\/ld\+json["'])[^>]*>[\s\S]*?<\/script>/gi, ''); |
| 21 | return { html, tags: [ |
| 22 | { tag: 'title', children: page.title }, |
| 23 | { tag: 'meta', attrs: { name: 'description', content: page.description } }, |
| 24 | ...Object.entries({ |
| 25 | 'og:title': page.socialTitle || page.title, 'og:description': page.description, |
| 26 | 'og:type': path.startsWith('/guides/') && path !== '/guides/' ? 'article' : 'website', |
| 27 | 'og:url': absolute(path), 'og:site_name': 'PulseCanvas Studio', 'og:locale': 'en_US', |
| 28 | 'og:image': absolute(socialImage.path), 'og:image:secure_url': absolute(socialImage.path), |
| 29 | 'og:image:type': socialImage.type, 'og:image:width': String(socialImage.width), |
| 30 | 'og:image:height': String(socialImage.height), 'og:image:alt': socialImage.alt, |
| 31 | }).map(([property, content]) => ({ tag: 'meta', attrs: { property, content } })), |
| 32 | ...Object.entries({ |
| 33 | 'twitter:card': 'summary_large_image', 'twitter:title': page.socialTitle || page.title, |
| 34 | 'twitter:description': page.description, 'twitter:image': absolute(socialImage.path), |
| 35 | 'twitter:image:alt': socialImage.alt, |
| 36 | }).map(([name, content]) => ({ tag: 'meta', attrs: { name, content } })), |
| 37 | ].map(tag => ({ ...tag, injectTo: 'head' })) }; |
| 38 | }, |
| 39 | }, |
| 40 | configureServer(server) { |
| 41 | server.middlewares.use((request, response, next) => { |
| 42 | const route = request.url?.split('?')[0]; |
| 43 | if (route !== '/robots.txt') return next(); |
| 44 | response.setHeader('Content-Type', 'text/plain'); |
| 45 | response.end(robots); |
| 46 | }); |
| 47 | }, |
| 48 | generateBundle() { |
| 49 | this.emitFile({ type: 'asset', fileName: 'robots.txt', source: robots }); |
| 50 | }, |
| 51 | }; |
| 52 | } |