Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hooks transformPageChunk html not replacing class #12150

Open
shinokada opened this issue Apr 22, 2024 · 1 comment
Open

hooks transformPageChunk html not replacing class #12150

shinokada opened this issue Apr 22, 2024 · 1 comment

Comments

@shinokada
Copy link

shinokada commented Apr 22, 2024

Describe the bug

I have the following hooks.server.ts. It replaces all words starting and ending with underbar, _ with 'world'.

It works for all the texts but it doesn't work for class.

// hooks.server.ts
export async function handle({ event, resolve }) {
	const response = await resolve(event, {
		transformPageChunk: ({ html }) => {
			const regex = /\b_\w+?_\b/g;

			return html.replaceAll(regex, 'world');
		}
	});

	return response;
}

+page.svelte

<h1 class="_main_h1_">Hello "_main_h1_"</h1>

Output:

<h1 class="_main_h1_" data-svelte-h="svelte-310lox">Hello "world"</h1>

If it is intended so, how can I apply replaceAll() to class names?

Reproduction

I create a simple stackblitz.

https://stackblitz.com/edit/sveltejs-kit-template-default-3msgtk?file=src%2Froutes%2F%2Bpage.svelte

Logs

No response

System Info

// stackblitz
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.3 - /usr/local/bin/pnpm
  npmPackages:
    svelte: ^4.2.7 => 4.2.15

Severity

annoyance

@7nik
Copy link

7nik commented Apr 22, 2024

If you check what the server gives away, you'll see that both _main_h1_ are replaced. But, at hydration, it gets fixed to match the component template. You can also see it by adding export const csr = false; to the +page.js. Not sure why only the class gets fixed.

Such use of transformPageChunk doesn't seem to be correct, or at least good, because it causes a hydration mismatch, and such substitution cannot be done for pages rendered on the client.

@Conduitry Conduitry transferred this issue from sveltejs/svelte Apr 22, 2024
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

No branches or pull requests

2 participants