Skip to content

feat: make rendered Slices discoverable in the DOM - #270

Open
filip-prismic wants to merge 1 commit into
masterfrom
filip/add-slice-comment-markers
Open

filip-prismic wants to merge 1 commit into
masterfrom
filip/add-slice-comment-markers

Conversation

@filip-prismic

@filip-prismic filip-prismic commented Sep 18, 2026

Copy link
Copy Markdown

Context

Live preview tooling needs a reliable way to identify each rendered Slice without requiring customer code changes or adding wrapper elements that could affect layout.

Related discussion: https://prismic-team.slack.com/archives/C0AUR4LLN2H/p1787730780921609

What this does

SliceZone adds client-side start and end comments around every Slice with an ID.
Even tho this is not the main requirement the comments support Slices that render not only one element, but also fragments (multiple elements) or no elements (null).

Server-rendered HTML remains unchanged, and GraphQL Slices without IDs remain unmarked.

How it works

A small client component renders React-owned text anchors after hydration, empties them before paint, and inserts HTML comments beside them. The comments are refreshed after React updates, so they follow reordered Slices and are removed with deleted Slices.

Playwright coverage verifies server output, hydration, fragments, empty Slices, reordering, and removal.

Comment thread tests/SliceZone.spec.ts
"prismic-slice-end:element-id",
])

await client.getByRole("button", { name: "Remove first" }).click()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 #idea: Rather than simulating client-side changes, we could do an E2E test. It would use a real preview update to ensure the IDs update in the actual scenario we are building.

I'll let you be the judge if that would be better though.

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.

2 participants