Skip to content

Extract snippets to external JS files#60

Merged
nucliweb merged 2 commits into
mainfrom
feat/external-snippets
Feb 25, 2026
Merged

Extract snippets to external JS files#60
nucliweb merged 2 commits into
mainfrom
feat/external-snippets

Conversation

@nucliweb

Copy link
Copy Markdown
Owner

Summary

All 48 JavaScript snippets are extracted from inline MDX code blocks into standalone .js files, making each snippet individually addressable by path — enabling LLMs and agents to reference, read, and analyze them directly.

Changes

New: snippets/ directory

48 .js files organized by category, mirroring the pages structure:

  • snippets/CoreWebVitals/ — 7 files (CLS, INP, LCP, LCP-Sub-Parts, LCP-Trail, LCP-Image-Entropy, LCP-Video-Candidate)
  • snippets/Interaction/ — 8 files
  • snippets/Loading/ — 27 files (including TTFB, TTFB-Sub-Parts, TTFB-Resources)
  • snippets/Media/ — 3 files
  • snippets/Resources/ — 1 file

Each file contains the raw, executable JavaScript as-is — valid for pasting directly into the browser DevTools console.

New: components/Snippet.jsx

React component that renders external snippet files with:

  • Syntax highlighting via highlight.js (light/dark mode)
  • Copy-to-clipboard button with visual feedback
  • max-height: 300px with vertical scroll to avoid long pages
  • Visual style matching Nextra's native code blocks

New: scripts/extract-snippets.js

One-time migration script that extracted all inline code blocks from ### Snippet sections in MDX files and rewrote them as import + <Snippet> usage. Kept in the repo for reference and future use.

Updated: all 46 MDX pages in pages/

Each page now imports its snippet(s) as raw strings and renders them via <Snippet>:

import snippet from '../../snippets/Loading/TTFB.js?raw'
import { Snippet } from '../../components/Snippet'

<Snippet code={snippet} />

For pages with multiple snippets (e.g. TTFB), each section imports its own file with a semantic name derived from the H2 heading.

Updated: next.config.js

Custom webpack rule to import .js files from snippets/ as raw strings via ?raw:

  • Injected into Next.js's oneOf block to take precedence over the SWC loader
  • Pre-loaders (React Fast Refresh) excluded from snippets/ to prevent HMR code injection
  • Result: import foo from './snippets/X.js?raw' yields the unmodified source string

Updated: styles/globals.css

  • Added highlight.js CSS for light mode (GitHub style) and dark mode (Atom One Dark)
  • Override for Nextra's pre code { padding: 0 !important } using .nextra-code-block pre code

Test plan

  • Check that all snippet pages render with syntax-highlighted code blocks
  • Verify copy button copies the original, unmodified snippet (no SWC transforms, no HMR code)
  • Confirm max-height scroll works on long snippets
  • Test light mode and dark mode syntax highlighting
  • Verify TTFB page renders 3 separate snippets correctly
  • Check that native Nextra code blocks (non-snippet pages) are unaffected

All 48 JavaScript snippets are extracted from inline MDX code blocks into
standalone .js files under snippets/{category}/, making them individually
addressable by LLMs and agents.

- Add snippets/ directory with 48 .js files organized by category
  (CoreWebVitals, Interaction, Loading, Media, Resources)
- Add components/Snippet.jsx: renders raw JS with highlight.js syntax
  highlighting, copy button, max-height scroll, and dark/light mode support
- Add scripts/extract-snippets.js: one-time migration script that extracted
  code blocks from ### Snippet sections and rewrote MDX imports
- Update all 46 MDX pages to import snippets via ?raw and use <Snippet>
- Update next.config.js: webpack asset/source rule for ?raw imports,
  injected into oneOf to take precedence over SWC, with pre-loaders
  excluded to prevent React Fast Refresh injection
- Update styles/globals.css: hljs light/dark themes, override Nextra's
  `pre code { padding: 0 !important }`
@vercel

vercel Bot commented Feb 24, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
webperf-snippets Ready Ready Preview, Comment Feb 25, 2026 10:11am
webperf-snippets-u6am Ready Ready Preview, Comment Feb 25, 2026 10:11am

@nucliweb nucliweb merged commit 637ebfe into main Feb 25, 2026
3 checks passed
@nucliweb nucliweb deleted the feat/external-snippets branch February 25, 2026 10:20
nucliweb added a commit that referenced this pull request Mar 2, 2026
Extract snippets to external JS files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant