Files
site/components/NoJs.jsx
Claude 7100c23b1b No error badge from JS-off styles: shared <NoJs> replaces <noscript> in the v2 kit
A <noscript> inside a client component cannot hydrate (the browser reads it as text with JS on),
which put Next's red error badge on every page using PinScenes, DetailSheet, MaskLines, the A2 and
Fam kits (seen on /comparison). components/NoJs.jsx writes the same rules as a plain <style>, each
selector scoped under html:not(.js) (html.js is set before first paint), so server and client
match and JS-off readers still get every rule. Verified: /comparison hydrates with no error; JS
off, 0 hidden text blocks on /comparison and /benchmarks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DqUybA3gCfaruBE8tifYd5
2026-11-07 15:03:02 +00:00

24 lines
1.4 KiB
React

/* <NoJs css="…" />: styles for visitors without JavaScript, with no <noscript>.
A <noscript> inside a client component cannot hydrate (React reads it as text in the browser), which puts Next's red
error badge on the page. This writes the same rules as a plain <style>, every selector scoped under html:not(.js);
app/layout.jsx sets html.js before first paint whenever JavaScript runs, so the rules apply only when it does not.
Server and client render the same string, so nothing mismatches. Works in server and client components.
Usage: <NoJs css=".my-step{opacity:1!important}" /> (plain CSS; @media blocks and keyframes are kept as they are). */
export function scopeNoJs(css) {
const src = String(css).replace(/<\/?style[^>]*>/g, '');
let out = '', i = 0;
while (i < src.length) {
const open = src.indexOf('{', i), close = src.indexOf('}', i);
if (open === -1) { out += src.slice(i); break; }
if (close !== -1 && close < open) { out += src.slice(i, close + 1); i = close + 1; continue; }
const head = src.slice(i, open), t = head.trim();
out += t.startsWith('@') || /^(from|to|\d+(\.\d+)?%)\b/.test(t) ? head + '{' : t.split(',').map((s) => 'html:not(.js) ' + s.trim()).join(',') + '{';
i = open + 1;
}
return out;
}
export default function NoJs({ css }) {
return <style dangerouslySetInnerHTML={{ __html: scopeNoJs(css) }} />;
}