-
Notifications
You must be signed in to change notification settings - Fork 5
feat: hydration issues across tabs and reloads #149
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
Open
huang-julien
wants to merge
14
commits into
main
Choose a base branch
from
feat/hydration_across_reload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c322afa
feat: hydration issues across tabs and reloads
huang-julien 3c5740f
add missing import
huang-julien a90df11
cleaning
huang-julien 9966e2f
fix: max 20 mismatches
huang-julien 8e961c1
fixes
huang-julien 6632e81
fix: tsconfigs
huang-julien 9b03f1f
use href
huang-julien 9a1a7d0
Update src/runtime/hydration/handler.nitro.ts
huang-julien 2dd433c
feat: refactor and add delete
huang-julien 30a7179
Merge branch 'feat/hydration_across_reload' of https://github.com/nuxβ¦
huang-julien d52b625
refactor: use variables
huang-julien d43ec65
fix: filepath
huang-julien 59255a9
fix: var name
huang-julien 5ffade5
Merge branch 'main' into feat/hydration_across_reload
huang-julien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { HydrationMismatchPayload, HydrationMismatchResponse, LocalHydrationMismatch } from '../../../src/runtime/hydration/types' | ||
| import { defineNuxtPlugin, useHostNuxt, ref } from '#imports' | ||
| import { HYDRATION_ROUTE, HYDRATION_SSE_ROUTE } from '../../../src/runtime/hydration/utils' | ||
|
|
||
| export default defineNuxtPlugin(() => { | ||
| const host = useHostNuxt() | ||
|
|
||
| const hydrationMismatches = ref<(HydrationMismatchPayload | LocalHydrationMismatch)[]>([]) | ||
|
|
||
| hydrationMismatches.value = [...host.__hints.hydration] | ||
|
|
||
| $fetch<HydrationMismatchResponse>(new URL(HYDRATION_ROUTE, window.location.origin).href).then((data: { mismatches: HydrationMismatchPayload[] }) => { | ||
| hydrationMismatches.value = [...hydrationMismatches.value, ...data.mismatches.filter(m => !hydrationMismatches.value.some(existing => existing.id === m.id))] | ||
| }) | ||
|
|
||
| const eventSource = new EventSource(new URL(HYDRATION_SSE_ROUTE, window.location.origin).href) | ||
| eventSource.addEventListener('hints:hydration:mismatch', (event) => { | ||
| const mismatch: HydrationMismatchPayload = JSON.parse(event.data) | ||
| if (!hydrationMismatches.value.some(existing => existing.id === mismatch.id)) { | ||
| hydrationMismatches.value.push(mismatch) | ||
| } | ||
| }) | ||
| eventSource.addEventListener('hints:hydration:cleared', (event) => { | ||
| const clearedIds: string[] = JSON.parse(event.data) | ||
| hydrationMismatches.value = hydrationMismatches.value.filter(m => !clearedIds.includes(m.id)) | ||
| }) | ||
huang-julien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| provide: { | ||
| hydrationMismatches, | ||
| }, | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| { | ||
| "extends": "./.nuxt/tsconfig.json", | ||
| "includes": [ | ||
| "../types.d.ts" | ||
| ] | ||
| "files": [], | ||
| "references": [ | ||
| { "path": "./.nuxt/tsconfig.app.json" }, | ||
| { "path": "./.nuxt/tsconfig.server.json" }, | ||
| { "path": "./.nuxt/tsconfig.shared.json" }, | ||
| { "path": "./.nuxt/tsconfig.node.json" } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type { H3Event } from 'h3' | ||
| import { createError, defineEventHandler, readBody, setResponseStatus } from 'h3' | ||
| import type { HydrationMismatchPayload } from './types' | ||
| import { useNitroApp } from 'nitropack/runtime' | ||
|
|
||
| const hydrationMismatches: HydrationMismatchPayload[] = [] | ||
|
|
||
| export default defineEventHandler((event) => { | ||
| switch (event.method) { | ||
| case 'GET': | ||
| return getHandler() | ||
| case 'POST': | ||
| return postHandler(event) | ||
| case 'DELETE': | ||
| return deleteHandler(event) | ||
| default: | ||
| throw createError({ statusCode: 405, statusMessage: 'Method Not Allowed' }) | ||
| } | ||
| }) | ||
|
|
||
| function getHandler() { | ||
| return { | ||
| mismatches: hydrationMismatches, | ||
| } | ||
| } | ||
|
|
||
| async function postHandler(event: H3Event) { | ||
| const body = await readBody<HydrationMismatchPayload>(event) | ||
| assertPayload(body) | ||
| const nitro = useNitroApp() | ||
| const payload = { id: body.id, htmlPreHydration: body.htmlPreHydration, htmlPostHydration: body.htmlPostHydration, componentName: body.componentName, fileLocation: body.fileLocation } | ||
| hydrationMismatches.push(payload) | ||
| if (hydrationMismatches.length > 20) { | ||
| hydrationMismatches.shift() | ||
| } | ||
| nitro.hooks.callHook('hints:hydration:mismatch', payload) | ||
| setResponseStatus(event, 201) | ||
| } | ||
|
|
||
| async function deleteHandler(event: H3Event) { | ||
| const nitro = useNitroApp() | ||
| const body = await readBody<{ id: string[] }>(event) | ||
| if (!body || !Array.isArray(body.id)) { | ||
| throw createError({ statusCode: 400, statusMessage: 'Invalid payload' }) | ||
| } | ||
| for (const id of body.id) { | ||
| const index = hydrationMismatches.findIndex(m => m.id === id) | ||
| if (index !== -1) { | ||
| hydrationMismatches.splice(index, 1) | ||
| } | ||
| } | ||
| nitro.hooks.callHook('hints:hydration:cleared', { id: body.id }) | ||
| setResponseStatus(event, 204) | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| function assertPayload(body: any): asserts body is HydrationMismatchPayload { | ||
| if ( | ||
| typeof body !== 'object' | ||
| || typeof body.id !== 'string' | ||
| || (body.htmlPreHydration !== undefined && typeof body.htmlPreHydration !== 'string') | ||
| || (body.htmlPostHydration !== undefined && typeof body.htmlPostHydration !== 'string') | ||
huang-julien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| || typeof body.componentName !== 'string' | ||
| || typeof body.fileLocation !== 'string' | ||
| ) { | ||
| throw createError({ statusCode: 400, statusMessage: 'Invalid payload' }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { createEventStream, defineEventHandler } from 'h3' | ||
| import { useNitroApp } from 'nitropack/runtime' | ||
|
|
||
| export default defineEventHandler((event) => { | ||
| const nitro = useNitroApp() | ||
| const eventStream = createEventStream(event) | ||
|
|
||
| const unsubs = [nitro.hooks.hook('hints:hydration:mismatch', (mismatch) => { | ||
| eventStream.push({ | ||
| data: JSON.stringify(mismatch), | ||
| event: 'hints:hydration:mismatch', | ||
| }) | ||
| }), nitro.hooks.hook('hints:hydration:cleared', async (payload) => { | ||
| eventStream.push({ | ||
| data: JSON.stringify(payload.id), | ||
| event: 'hints:hydration:cleared', | ||
| }) | ||
| })] | ||
|
|
||
| eventStream.onClosed(async () => { | ||
| unsubs.forEach(unsub => unsub()) | ||
| await eventStream.close() | ||
| }) | ||
|
|
||
| return eventStream.send() | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.