Map Rendered Text to File, Line and Column in a Next.js 16 App
Map Rendered Text to File, Line and Column in a Next.js 16 App
Combine code-inspector-plugin stamps with a TypeScript compiler string index to trace rendered copy back to its exact…
·Updated on:··
⚡ Next.js Implementation Guides
In-depth Next.js guides covering App Router, RSC, ISR, and deployment. Get code examples, optimization checklists, and prompts to accelerate development.
By Matija, the developer behind Build with Matija. Tested with Next.js 16.3.8 (App Router, Turbopack), code-inspector-plugin 2.0.9, TypeScript 5.9 and Playwright 1.56. Last updated 2026-10-23.
You can find the exact file, line and column behind any piece of text on a rendered Next.js page by combining two things. The first is code-inspector-plugin, gated behind an environment variable, so a preview build stamps every element with data-insp-path="file:line:column". The second is a source index built with the TypeScript compiler, listing every string written in the repository with its position. Read the stamps from the rendered HTML, look each text up in the index, and narrow duplicates using the element's own file, the modules the page imports, and the link next to the text. On five pages of a production marketing site this placed 61% of all strings at their exact line, with every one of those locations checked against the files. This guide shows the build step, the index, the matching, and the numbers.
Why I needed the line of code behind a string
I'm building Attest, a tool for translating a website in context: English on the left, the French appearing in place on the right, a reviewer approving each string, and an export at the end. The first client is a Next.js marketing site that gets a French version, and the export is the handover. Whoever applies it, a developer or an AI agent, has to know where each string lives in the code. "Learn more" appears forty times on that site. Without a location, every one of those strings is a question.
The obvious tools did not fit. Production source maps map generated JavaScript, and the text on a server-rendered page never passes through them. React's owner stacks and the Next.js dev overlay only exist in development and point at whole components. Stega encoding (hiding invisible characters inside strings, as Vercel's content tools do) changes the strings themselves, which breaks hrefs, slugs and length checks. What I wanted was a build-time stamp on every element and an index of every string, so the mapping happens outside the site's runtime entirely.
Two locations for every string
Before any code, it helps to separate two places a piece of text comes from. The render location is the JSX element that displays it, for example src/components/cards/ArticleCard.tsx:27:5. The content origin is where the string itself is written, for example src/data/articles.ts:12:14. For a heading typed directly into JSX, the two are the same line. For a product name that comes from a JSON file and passes through three components, they are in different files.
The rest of this guide builds those three layers in order.
Step 1: Stamp every element in a flagged build
code-inspector-plugin is a dev tool: in next dev you hold a key, click an element, and your editor opens at its source. It does that by transforming every JSX element at build time to carry a data-insp-path attribute. That attribute is exactly what we need, in a production build, and only when we ask for it.
This adds Turbopack loader rules only when ATTEST_SOURCE=1 is set, so the production deployment builds exactly as before and one preview environment carries the stamps. The dev: true option is the part that cost me an hour. The plugin checks whether it is running in development and quietly returns an empty rule set otherwise. A flagged next build then succeeds and produces HTML with no stamps at all, with no warning. Forcing dev: true is what makes it work in a production build.
Under Next.js 16 the plugin's Turbopack integration runs its loader over every .js, .ts, .jsx and .tsx module, so Server Components are covered as well as client components. That was the claim I most needed to verify, because the site's pages are Server Components rendered on demand. A flagged build of the client's site served 1,637 stamps on a single page, and 1,100 of the 1,108 visible strings I measured had a stamp on their own element. The other 8 had one on a parent.
Two details showed up in the output. The paths are absolute even with pathType: "relative", so a stamp looks like /home/build/site/src/components/ui/Button.tsx:57:9:span. They also end with the element or component name. Both are easy to handle when reading them, which is the next step. A build without the flag served no data-insp-path at all. The word code-inspector only showed up in the build cache and TypeScript's build info, because next.config.ts imports the plugin. If you want even that gone, import the plugin dynamically inside the flag check.
For more on what changed around Turbopack in this release line, see my notes on what's new in Next.js 16.3.
Step 2: Read the stamps from the rendered page
With a flagged preview deployed, the stamps are plain attributes in the HTML. To pair them with text, you need the page as a browser lays it out, because what counts as one string depends on layout. In Attest, a headless Chromium renders the page, a segmenter splits it into blocks (a paragraph or heading with its inline links becomes one string), and each block's stamp is read from its element.
The selector list here is a simplification of what Attest's segmenter does, which also handles mixed content and hidden menus, but the principle is the same. closest("[data-insp-path]") takes the element's own stamp when it has one, and its parent's otherwise. The build root is stripped so the result is a repository-relative path. In Attest, the build root comes from the source index in the next step, so nothing about the build machine leaks into the export.
At this point every string on the page knows which component rendered it. For a heading typed into JSX, that is already the answer. For anything that came from data, it is only half of it.
Step 3: Index every string in the repository
The render location of a product card title is ProductCard.tsx, but the title itself is written in a JSON file or a data module. To find it, the build also writes an index of every string in the repository with its position. The TypeScript compiler API does the parsing, which means the index sees exactly the JSX text, attribute strings, string literals and template literals the compiler sees.
Each indexed entry carries the raw text, a normalized form for exact lookups, its repository-relative file, line and column, and its kind. Templates keep their static parts separated by a null character, so Showing ${n} products can later match "Showing 12 products" as a pattern. The real index also skips strings that are code rather than copy (class names, keys, comparisons) and covers content/**/*.json through ts.parseJsonText and Markdown paragraphs from content/**/*.md. On the client's site, most of the copy lives in JSON: 19,750 of the 29,438 indexed strings.
The lineAndColumn helper looks like it reinvents sf.getLineAndCharacterOfPosition, and it does, on purpose. Every location in one JSON file was off by exactly one line after a certain point. The cause was a single U+2028 (line separator) character inside a product description. TypeScript counts U+2028 and U+2029 as line breaks, while editors and git only count \n. Counting lines yourself fixes it, and it is the kind of bug you only find by checking every result against the file, which is what I did in step 5.
Two more things go into the index for the next step: each object literal's string values, so a title can be found through the href or slug beside it, and each module's resolved imports. The index is written once per build, in the same flagged build, and published with the preview at a path like /.attest/source-index.json.
Step 4: Match each piece of text to the code
Now every string on the page has a render location from step 2, and every string in the repository has a position from step 3. Matching joins them. A text that appears exactly once in the index is settled immediately. Short labels are the hard part: "Products" might be written in the navigation JSON, a mega-menu module and a footer component.
The narrowing runs in three steps, and each one only applies if it leaves at least one candidate. First, a literal in the same file as the element that renders it wins, which settles JSX text and component-local labels. Second, the candidates are limited to modules the page's route actually reaches through imports. Data files read with fs at runtime never show up as imports, so content/ stays in. Third, the candidates are limited to object literals whose other values contain the text's own link. A menu entry "Immunity" sitting in an <a href="/goals/immunity"> matches the { label: "Immunity", href: "/goals/immunity" } object and none of the other "Immunity" strings.
Anything still tied stays ambiguous, with the candidates listed, which is honest and still useful: two or three locations are far better than a search across the repository. Templates give "composed" with the template's location. Text not in the index at all, such as content from a CMS or a product feed, is "external" and keeps its render location.
Step 5: What the numbers looked like on a real site
I ran all of this on five pages of the client's site (home, a health goal page, about, a product and a blog post), built locally with the flag. That was 1,439 translatable strings: 1,108 visible and 331 attribute or meta texts such as alt and page descriptions.
The exact share is the number that matters, so I checked it: for all 881 exact matches, the text starts at the recorded file, line and column. That check is also how the U+2028 bug from step 3 surfaced. Before narrowing, only 48% were exact; the import and link steps brought that to 61%. Per page it ranged from 46% on the health goal page, which is heavy with menus and filters, to 73% on the about page.
The 23% ambiguous are almost all short labels. Of the 519 ambiguous strings before narrowing, 448 had three words or fewer: menu entries, product names, button labels. After narrowing, 246 of the remaining 329 have only two or three candidates, and 79 are the same text repeated in one list of one file. The next lever for those is the React Server Components payload, which carries the props each client component received and therefore the data path a string came through. I did not need it to make the approach worth building.
Step 6: How the translation tool uses it
In Attest, all of this collapses into one field per occurrence in the JSON export. The export format already has that field. The capture worker is being extended to fill it: it reads data-insp-path while it renders the page, strips the build root, removes the attribute from the stored snapshot so the translators' view stays clean, loads the source index once per snapshot, and stores each string's match. An occurrence in the export then looks like this (the paths are illustrative):
json
A developer or an AI agent applying the French reads origin and goes straight to line 118 of content/goals.json. If is , lists the two or three places to choose from. If it is , the string is not in the repository, and the render location says which component displays it. The export never depends on how the site stores its content, which is what lets the same tool work for a JSON-driven site and one backed by a CMS. If your site does use Payload, my guide to covers the CMS side of editing in context, and covers machine translation, which is the opposite end from the human-reviewed flow here.
To be clear about where this stands: the measurements above come from a spike run against a local flagged build of the client's site. Building it into Attest's capture and export is the work in progress now, and the flagged preview on the client's side needs their agreement before it goes into their repository.
FAQ
Does code-inspector-plugin work with webpack instead of Turbopack?
Yes, it supports webpack, Vite, Rspack and others with the same data-insp-path attribute. I only tested the Turbopack integration under Next.js 16, which is what the site uses, and there the dev: true option is what makes it run in a production build.
Does the flag slow down or change the production site?
No. The plugin's rules are only added when ATTEST_SOURCE=1 is set, and that variable exists on one preview environment. A build without it served no stamps. The flagged build's HTML is larger, about 19% on one page, because every element carries an absolute path.
Do client components get stamps too?
The loader transforms every module, server or client, so their elements carry the attribute in the HTML the server renders. The pages I measured were Server Components rendered on demand, and 1,100 of 1,108 visible strings had a stamp on their own element.
What about text that comes from a CMS or an API?
It is not in the repository, so it matches as external, keeps its render location (which component displays it), and is placed in the export by page and CSS path. On this site that was 7% of strings.
Why not use invisible markers in the strings instead?
Stega-style encoding changes the strings themselves, which can break links, slugs, comparisons and length checks. It also adds a lot of characters to every payload. Stamps on elements plus a separate index leave the site's strings untouched.
Conclusion
The problem was handing a translated website back to developers and AI agents with every string tied to the place it lives in the code. Two build-time artifacts solved it: code-inspector-plugin, forced on with dev: true behind an environment flag so every element carries its render location, and a TypeScript-compiler source index of every string in the repository. A matching step narrows duplicates by the element's file, the page's imports and the text's own link. On a real Next.js 16 site, 61% of strings landed on their exact line, every one verified, and most of the rest narrowed to two or three places.
You can now stamp a Next.js build without touching production, read render locations from the HTML, index a repository's copy with the TypeScript compiler, and match the two, including the line-counting trap that comes with real content.
Let me know in the comments if you have questions, and subscribe for more practical development guides.
Thanks, Matija
Layer
What it gives
How
Needs
Element stamps
Render location of every host element
code-inspector-plugin adds data-insp-path at build time
A flag-gated build step
Source index
Content origin of every string literal
TypeScript compiler API over src/ and content/
The same build
Matching
One location per string, or a short list
Exact lookup, then narrowing
Both of the above
// File: next.config.ts
import
type
NextConfig
from
"next"
import
from
"code-inspector-plugin"
// Element stamps exist only in a build with ATTEST_SOURCE=1.
const
env
ATTEST_SOURCE
"1"
const
nextConfig
NextConfig
turbopack
rules
codeInspectorPlugin
bundler
"turbopack"
dev
true
// required: without it the plugin returns no rules outside `next dev`
server
"close"
// no local click-to-open server
printServer
false
pathType
"relative"
export
default
// File: scripts/read-stamps.ts
import
from
"playwright-core"
type
Stamped
text
string
file
string
line
number
column
number
element
string
/** Every visible text block on the page with the render location of its element. */