fix: 前缀代理 HTML 改写只挂根绝对 URL,消除 hydration href 分叉 (#55)
Build Tutorial / Build @stack/tutorial (kaniko) (push) Successful in 4m52s
Build Tutorial / Build @stack/tutorial (kaniko) (push) Successful in 4m52s
## 背景 `dev:proxy`(`--base /absproxy/8910`)下打开任意页面,React 控制台报 hydration 不匹配: `Warning: Prop `href` did not match. Server: "/absproxy/8910/index.html" Client: "../index.html"` 根因:SSG 预渲染与客户端 `relHref`(纯函数)两端同算相对链接,本无分叉;但 dev/preview 伺服时 `rewriteWithBase` 把 HTML 里**所有** href/src(含相对)规范成 base 前缀绝对路径,服务端 HTML 与客户端 hydration 值就此分家。 ## 变更内容 - `packages/stack/src/node/baseHtml.ts`:`rewriteWithBase` 只改写根绝对 URL(挂 base、规范化、保留 query/hash)——这才是会逃逸到域名根、必须挂前缀的形态;相对 URL 原样保留(页面 URL 已含 base 前缀,相对解析天然落在代理内),外部链接/锚点/data URI/协议相对照旧不动。卸下 `pagePath` 参数(相对解析职责随相对改写一起退场) - `packages/stack/src/node/dev.ts`:调用点随签名收敛 - `packages/stack/test/baseHtml.spec.ts`:契约随迁四例(根绝对挂 base/相对保留保 hydration 一致/外部类原样/base 空时根绝对不变) ## 自测结果 - 单测:`packages/stack` `node --test` 74/74 全绿;typecheck 通过 - 8913 端口起 `stack dev --base /absproxy/8910` 实测: - `/absproxy/8910/` 与 `/absproxy/8910/skills/css.html` 的 `#root` 内链接全部保持相对(`./index.html`、`../index.html`),0 条被改写成 `/absproxy/…` - 相对资源可达:`/absproxy/8910/src/design-system/styles/tokens.css` 返回 200,样式正常 - playwright 打开 css 页收集 console:**hydration 警告 0 条**,DOM 中 brand href 为 `../index.html`,body 纸底色正常渲染 --------- Co-authored-by: cqh963852 <cqh963852@hotmail.com> Reviewed-on: #55
This commit was merged in pull request #55.
This commit is contained in:
1 parent
11b8f8328f
commit
0b2656689f
3 files changed
+19
-30
No files matched your search
@@ -1,15 +1,10 @@
|
||||
const NON_LOCAL_PREFIXES = ['//', 'http://', 'https://', 'data:', 'mailto:', 'tel:']
|
||||
|
||||
export function rewriteWithBase(html: string, base: string, pagePath: string): string {
|
||||
const pageDir = pagePath.endsWith('/')
|
||||
? pagePath
|
||||
: pagePath.slice(0, pagePath.lastIndexOf('/') + 1)
|
||||
export function rewriteWithBase(html: string, base: string): string {
|
||||
return html.replace(/(\s(?:href|src))="([^"]*)"/g, (match, attribute: string, value: string) => {
|
||||
if (value === '' || value.startsWith('#')) return match
|
||||
if (!value.startsWith('/')) return match
|
||||
if (NON_LOCAL_PREFIXES.some((prefix) => value.startsWith(prefix))) return match
|
||||
const resolved = value.startsWith('/')
|
||||
? new URL(`${base}${value}`, 'http://localhost')
|
||||
: new URL(value, `http://localhost${base}${pageDir}`)
|
||||
const resolved = new URL(`${base}${value}`, 'http://localhost')
|
||||
return `${attribute}="${resolved.pathname}${resolved.search}${resolved.hash}"`
|
||||
})
|
||||
}
|
||||
@@ -123,7 +123,7 @@ export async function startDevServer(
|
||||
const isHtml = served.contentType.startsWith('text/html')
|
||||
let code: string | Uint8Array = served.code
|
||||
if (isHtml && typeof code === 'string') {
|
||||
let text = base === '' ? code : rewriteWithBase(code, base, withoutBase)
|
||||
let text = base === '' ? code : rewriteWithBase(code, base)
|
||||
if (options.liveReload === true) text = text.replace(/<\/body>/i, `${liveScript}</body>`)
|
||||
code = text
|
||||
}
|
||||
|
||||
@@ -2,35 +2,29 @@ import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { rewriteWithBase } from '../src/node/baseHtml.ts'
|
||||
|
||||
test('相对 URL 对齐 base 与页面目录', () => {
|
||||
const html = '<link href="./src/a.css" /><script src="./main.js"></script>'
|
||||
const rewritten = rewriteWithBase(html, '/proxy/9910', '/')
|
||||
test('根绝对 URL 挂 base 前缀,query 与 hash 保留', () => {
|
||||
const html = '<a href="/index.html"><script src="/assets/main.js?v=2"></script>'
|
||||
const rewritten = rewriteWithBase(html, '/absproxy/9910')
|
||||
assert.equal(
|
||||
rewritten,
|
||||
'<link href="/proxy/9910/src/a.css" /><script src="/proxy/9910/main.js"></script>',
|
||||
'<a href="/absproxy/9910/index.html"><script src="/absproxy/9910/assets/main.js?v=2"></script>',
|
||||
)
|
||||
})
|
||||
|
||||
test('内部绝对 URL 直接挂 base,跨层页面的相对 URL 按自身目录解析', () => {
|
||||
const html = '<a href="/index.html"><img src="../shared/logo.png" /></a>'
|
||||
const rewritten = rewriteWithBase(html, '/absproxy/9910', '/learn/deep/index.html')
|
||||
assert.equal(
|
||||
rewritten,
|
||||
'<a href="/absproxy/9910/index.html"><img src="/absproxy/9910/learn/shared/logo.png" /></a>',
|
||||
)
|
||||
test('相对 URL 原样保留——页面 URL 已含 base 前缀,相对解析天然落在代理内,且与 SSG 预渲染的 href 一致、hydration 不分叉', () => {
|
||||
const html = '<a href="../index.html"><link href="./src/a.css" /><img src="../shared/logo.png" />'
|
||||
const rewritten = rewriteWithBase(html, '/absproxy/9910')
|
||||
assert.equal(rewritten, html)
|
||||
})
|
||||
|
||||
test('外部链接、锚点、data URI、query 与 hash 原样保留', () => {
|
||||
test('外部链接、锚点、data URI、协议相对 URL 原样保留', () => {
|
||||
const html =
|
||||
'<a href="https://idealjs.com">x</a><a href="#top">y</a><img src="data:image/png;base64,AA" /><a href="./a.html?v=2#s">z</a>'
|
||||
const rewritten = rewriteWithBase(html, '/proxy/9910', '/')
|
||||
assert.ok(rewritten.includes('href="https://idealjs.com"'))
|
||||
assert.ok(rewritten.includes('href="#top"'))
|
||||
assert.ok(rewritten.includes('src="data:image/png;base64,AA"'))
|
||||
assert.ok(rewritten.includes('href="/proxy/9910/a.html?v=2#s"'))
|
||||
'<a href="https://idealjs.com">x</a><a href="#top">y</a><img src="data:image/png;base64,AA" /><a href="//cdn.example.com/a.js">z</a>'
|
||||
const rewritten = rewriteWithBase(html, '/proxy/9910')
|
||||
assert.equal(rewritten, html)
|
||||
})
|
||||
|
||||
test('base 为空时站内 URL 仍规范为根绝对路径', () => {
|
||||
const rewritten = rewriteWithBase('<a href="/index.html"><img src="./logo.png" /></a>', '', '/')
|
||||
assert.equal(rewritten, '<a href="/index.html"><img src="/logo.png" /></a>')
|
||||
test('base 为空时根绝对 URL 规范化后不变', () => {
|
||||
const rewritten = rewriteWithBase('<a href="/index.html">', '')
|
||||
assert.equal(rewritten, '<a href="/index.html">')
|
||||
})
|
||||
Reference in new issue
Block a user