{"record":{"id":"987d2acf3a5ebb2f","repo":"honojs/hono","slug":"style-sheet-not-found","errorCode":null,"errorMessage":"style sheet not found","messagePattern":"style sheet not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jsx/dom/css.ts","lineNumber":97,"sourceCode":"  const findStyleSheet = (): [CSSStyleSheet, Set<string>] | [] => {\n    if (!styleSheet) {\n      styleSheet = document.querySelector<HTMLStyleElement>(`style#${id}`)\n        ?.sheet as CSSStyleSheet | null\n      if (styleSheet) {\n        // eslint-disable-next-line @typescript-eslint/no-explicit-any\n        ;(styleSheet as any).addedStyles = new Set<string>()\n      }\n    }\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return styleSheet ? [styleSheet, (styleSheet as any).addedStyles] : []\n  }\n\n  const insertRule = (className: string, styleString: string) => {\n    const [sheet, addedStyles] = findStyleSheet()\n    if (!sheet || !addedStyles) {\n      Promise.resolve().then(() => {\n        if (!findStyleSheet()[0]) {\n          throw new Error('style sheet not found')\n        }\n        insertRule(className, styleString)\n      })\n      return\n    }\n\n    if (!addedStyles.has(className)) {\n      addedStyles.add(className)\n      ;(className.startsWith(PSEUDO_GLOBAL_SELECTOR)\n        ? splitRule(styleString)\n        : [`${className[0] === '@' ? '' : '.'}${className}{${styleString}}`]\n      ).forEach((rule) => {\n        sheet.insertRule(rule, sheet.cssRules.length)\n      })\n    }\n  }\n\n  const cssObject = {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/honojs/hono/blob/e2740d5a1bd0b4254e517e3af8b60789284bc7bd/src/jsx/dom/css.ts#L79-L115","documentation":"hono/jsx/dom/css implements CSS Modules by inserting rules into a shared style sheet that must exist in the document. insertRule looks up the style element via findStyleSheet; if none is found synchronously it retries on a microtask, and throws if the sheet still cannot be found — typically because the code runs where no style element/document exists (e.g. SSR or a stripped head).","triggerScenarios":"Importing a CSS module (e.g. `import styles from './x.module.css'`) and using its class names in a context where the injected <style> element is absent — server-side rendering, a document whose style element was removed, or calling css APIs before the sheet helper has installed the element.","commonSituations":"Running hono/jsx DOM components during SSR/build where document is unavailable; third-party code or cleanup logic removing the style tag; timing issues where the sheet is queried after the element was detached.","solutions":["Ensure the hono/jsx style-sheet style element is present in the document before rendering components that use CSS modules","Avoid importing .module.css or triggering insertRule during SSR; restrict CSS-module usage to client-rendered code paths","If you remove/replace style elements manually, re-create one (the expected style element) or re-mount before rendering"],"exampleFix":"// before\n// SSR path imports component that uses styles from './Button.module.css'\nimport styles from './Button.module.css'\n// after\nlet styles = { btn: 'fallback-btn-class' }\nif (typeof document !== 'undefined') {\n  styles = (await import('./Button.module.css')).default\n}","handlingStrategy":"type-guard","validationCode":"const canUseCssModules =\n  typeof document !== 'undefined' &&\n  !!document.querySelector('style')","typeGuard":"const hasStyleSheet = (): boolean => typeof document !== 'undefined' && !!findStyleSheetLike()","tryCatchPattern":"try {\n  element.classList.add(styles.btn)\n} catch (e) {\n  if (e instanceof Error && /style sheet not found/.test(e.message)) {\n    // fall back to a static class name\n  }\n}","preventionTips":["Only import CSS modules in client-rendered code paths","Don't remove the framework-managed style element from the DOM","Lazy-load .module.css imports behind a document check in isomorphic code"],"tags":["hono","jsx","css-modules","dom","ssr"],"backgroundTag":"stylesheet-not-found","analyzedSha":"e2740d5a1bd0b4254e517e3af8b60789284bc7bd","analyzedAt":"2026-08-28T10:18:08.750Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}