Skip to content

Commit

Permalink
fix: disallow superfluous elements in <html> before react hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Apr 21, 2024
1 parent 738812e commit 16b239f
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 176 deletions.
15 changes: 8 additions & 7 deletions app.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const customVite = () =>
'react-icons',
],
},
resolve:
env.command !== 'build'
? {
dedupe: [
resolve: {
dedupe: [
...(env.command !== 'build'
? [
'react',
'react-dom',
'@tanstack/store',
Expand All @@ -34,9 +34,10 @@ const customVite = () =>
'@tanstack/react-cross-context',
'@tanstack/history',
'use-sync-external-store',
],
}
: {},
]
: []),
],
},
// plugins: [
// {
// name: 'inline-env-vars-as-prefix',
Expand Down
43 changes: 25 additions & 18 deletions app/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,10 @@ export default eventHandler(async (event) => {
}) as any

if (import.meta.env.DEV) {
assets.push(
{
tag: 'script',
attrs: {},
children: `
// remove all script tags with src containing chrome-extension
document.querySelectorAll('script').forEach((script) => {
if (script.src.includes('chrome-extension')) {
script.remove()
}
})
`,
},
{
tag: 'script',
children: `window.__vite_plugin_react_preamble_installed__ = true`,
}
)
assets.push({
tag: 'script',
children: `window.__vite_plugin_react_preamble_installed__ = true`,
})
}

assets.push({
Expand All @@ -62,6 +48,27 @@ export default eventHandler(async (event) => {
},
})

assets.push({
tag: 'script',
children: `
let htmlElement = document.documentElement;
let childNodes = Array.from(htmlElement.childNodes);
let removedElements = [];
childNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE && node.tagName !== 'HEAD' && node.tagName !== 'BODY') {
// Remove the node
removedElements.push(node);
htmlElement.removeChild(node);
}
});
if (removedElements.length > 0) {
console.warn(
'Removed elements from <html> to prevent hydration mismatches with React.',
removedElements
);
}`,
})

if (import.meta.env.DEV) {
assets.push({
tag: 'script',
Expand Down
Binary file modified media/brand.sketch
Binary file not shown.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"@oramacloud/client": "^1.0.14",
"@remix-run/node": "^2.8.1",
"@tailwindcss/typography": "^0.5.10",
"@tanstack/react-router": "1.28.7",
"@tanstack/react-router-server": "1.28.7",
"@tanstack/router-devtools": "1.28.7",
"@tanstack/router-vite-plugin": "1.28.2",
"@tanstack/react-router": "1.29.2",
"@tanstack/react-router-server": "1.29.2",
"@tanstack/router-devtools": "1.29.2",
"@tanstack/router-vite-plugin": "1.30.0",
"@typescript-eslint/parser": "^7.2.0",
"@vinxi/react": "0.2.2",
"@vinxi/server-functions": "^0.3.2",
Expand Down Expand Up @@ -63,7 +63,7 @@
"zustand": "^4.5.2"
},
"devDependencies": {
"@orama/orama": "^2.0.15",
"@orama/orama": "^2.0.16",
"@types/react": "^18.2.65",
"@types/react-dom": "^18.2.21",
"@types/remove-markdown": "^0.3.4",
Expand Down
Loading

0 comments on commit 16b239f

Please sign in to comment.