isr-engine 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli/cli.js +1 -1
- package/dist/config/defineConfig.d.ts +1 -1
- package/dist/config/defineConfig.d.ts.map +1 -1
- package/dist/config/defineConfig.js +2 -1
- package/dist/config/defineConfig.js.map +1 -1
- package/dist/cookie-DcUeYm6E.js +42 -0
- package/dist/cookie-DcUeYm6E.js.map +1 -0
- package/dist/{createCacheInspectorMiddleware-C2QOaq2z.js → createCacheInspectorMiddleware-drK-Mk-D.js} +2 -2
- package/dist/{createCacheInspectorMiddleware-C2QOaq2z.js.map → createCacheInspectorMiddleware-drK-Mk-D.js.map} +1 -1
- package/dist/{createISRApp-CR1EeHfI.js → createISRApp-CfxiYTiH.js} +6 -5
- package/dist/{createISRApp-CR1EeHfI.js.map → createISRApp-CfxiYTiH.js.map} +1 -1
- package/dist/defaults/runtime/defineSiteHooks.js +2 -2
- package/dist/{defineSiteHooks-DUEds_hB.js → defineSiteHooks-CLmYrmzS.js} +3 -2
- package/dist/{defineSiteHooks-DUEds_hB.js.map → defineSiteHooks-CLmYrmzS.js.map} +1 -1
- package/dist/{i18n-CJ0leKhc.js → i18n-D4w2mBvr.js} +32 -41
- package/dist/i18n-D4w2mBvr.js.map +1 -0
- package/dist/isr-engine.js +4 -3
- package/dist/isr-engine.js.map +1 -1
- package/package.json +1 -1
- package/dist/i18n-CJ0leKhc.js.map +0 -1
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"i18n-CJ0leKhc.js","names":[],"sources":["../src/utils/cookie.ts","../src/utils/trimTrailingSlashes.ts","../src/runtime/i18n.ts"],"sourcesContent":["export type CookieHeaderSource =\n | string\n | null\n | undefined\n | {\n headers?: Headers | Record<string, string | string[] | number | undefined | null>;\n };\n\nexport function getCookieHeader(source: CookieHeaderSource): string {\n if (!source) return '';\n if (typeof source === 'string') return source;\n\n const headers = source.headers;\n if (!headers) return '';\n\n if (typeof (headers as Headers).get === 'function') {\n return (headers as Headers).get('cookie') ?? '';\n }\n\n const record = headers as Record<string, string | string[] | number | undefined | null>;\n const raw = record.cookie ?? record.Cookie;\n if (Array.isArray(raw)) return raw.join('; ');\n return raw === undefined || raw === null ? '' : String(raw);\n}\n\nexport function readCookie(source: CookieHeaderSource, name: string): string | undefined {\n if (!name) return undefined;\n const cookies = parseCookieHeader(getCookieHeader(source));\n return cookies[name];\n}\n\nexport function parseCookieHeader(header: string): Record<string, string> {\n const entries = new Map<string, string>();\n\n for (const part of header.split(';')) {\n const trimmed = part.trim();\n if (!trimmed) continue;\n\n const index = trimmed.indexOf('=');\n if (index <= 0) continue;\n\n const key = trimmed.slice(0, index).trim();\n if (!key) continue;\n\n const rawValue = trimmed.slice(index + 1).trim();\n entries.set(key, safeDecodeCookieValue(rawValue));\n }\n\n // fromEntries creates own data properties, including __proto__, without invoking setters.\n const cookies = Object.fromEntries(entries);\n Object.setPrototypeOf(cookies, null);\n return cookies;\n}\n\nfunction safeDecodeCookieValue(value: string): string {\n try {\n return decodeURIComponent(value);\n } catch {\n return value;\n }\n}\n","/** Remove only the trailing slash run; each character is inspected at most once. */\nexport function trimTrailingSlashes(value: string): string {\n let end = value.length;\n while (end > 0 && value[end - 1] === '/') end--;\n return value.slice(0, end);\n}\n","/**\n * i18n 路由工具 —— URL 前缀自动处理(无 React 依赖,server + client 通用)\n *\n * 业务侧用法:\n *\n * import { parseLocale, withLocale, negotiateLocale } from 'isr-engine/runtime';\n *\n * const I18N = { locales: ['zh', 'en'] as const, defaultLocale: 'zh' };\n *\n * // SSR 入口:从 URL 拆 locale + 干净 pathname 给路由\n * const { locale, pathname } = parseLocale(url.pathname, I18N);\n * return <Layout lang={locale}>{resolveRoute({ pathname, locale })}</Layout>;\n *\n * // Header 切换语言\n * const href = withLocale('/books/1', 'en', I18N); // → '/en/books/1'\n * const href = withLocale('/books/1', 'zh', I18N); // → '/books/1'(默认 locale 不前缀)\n *\n * // 服务器协商:Accept-Language 头 → locale\n * const locale = negotiateLocale(req.headers.get('accept-language'), I18N);\n *\n * 设计取舍:\n * - 默认 locale 不带前缀('/about'),其他 locale 带('/en/about')\n * → 与 Next.js i18n routing 默认 + Vue I18n 一致;SEO 友好(默认语言 URL 简洁)\n * - 用 `prefixDefault: true` 切到\"全部带前缀\"模式(如 '/zh/about')\n * - 不内置消息字典 / 翻译机制 —— 那是 react-intl / FormatJS / lingui 的活\n */\n\nimport { trimTrailingSlashes } from '../utils/trimTrailingSlashes';\nexport interface I18nConfig {\n /** 支持的 locale 列表,如 ['zh', 'en', 'ja'] */\n locales: readonly string[];\n /** 默认 locale —— 必须在 locales 内 */\n defaultLocale: string;\n /** 是否给默认 locale 也加 URL 前缀(默认 false:'/about';true:'/zh/about') */\n prefixDefault?: boolean;\n}\n\nexport interface ParsedLocale {\n /** 解析出的 locale(命中 URL 前缀则取前缀,否则取 defaultLocale) */\n locale: string;\n /** 去掉 locale 前缀后的\"业务 pathname\",路由表只需匹配这个 */\n pathname: string;\n /** URL 是否真的带了 locale 前缀 */\n hasPrefix: boolean;\n}\n\n/**\n * 从 pathname 拆出 locale 与业务 pathname\n *\n * '/zh/books/1' + { locales: ['zh','en'], defaultLocale: 'en' }\n * → { locale: 'zh', pathname: '/books/1', hasPrefix: true }\n *\n * '/books/1' 同上\n * → { locale: 'en', pathname: '/books/1', hasPrefix: false }\n */\nexport function parseLocale(pathname: string, config: I18nConfig): ParsedLocale {\n const segments = pathname.split('/');\n // segments[0] 是空串(leading '/'),segments[1] 才是首段\n const first = segments[1];\n if (first && config.locales.includes(first)) {\n const rest = '/' + segments.slice(2).join('/');\n return {\n locale: first,\n pathname: rest === '/' ? '/' : trimTrailingSlashes(rest),\n hasPrefix: true,\n };\n }\n return { locale: config.defaultLocale, pathname: pathname || '/', hasPrefix: false };\n}\n\n/**\n * 给业务 pathname 套上 locale 前缀(生成可跳转 URL)\n *\n * withLocale('/about', 'en', { defaultLocale: 'zh', locales: ['zh','en'] })\n * → '/en/about'\n *\n * withLocale('/about', 'zh', { defaultLocale: 'zh', locales: ['zh','en'] })\n * → '/about' (默认 locale 不前缀)\n *\n * withLocale('/about', 'zh', { ..., prefixDefault: true })\n * → '/zh/about'\n */\nexport function withLocale(pathname: string, locale: string, config: I18nConfig): string {\n if (!config.locales.includes(locale)) {\n throw new Error(`[i18n] unknown locale \"${locale}\"; allowed: ${config.locales.join(', ')}`);\n }\n const clean = pathname.startsWith('/') ? pathname : '/' + pathname;\n if (locale === config.defaultLocale && !config.prefixDefault) return clean;\n return `/${locale}${clean === '/' ? '' : clean}`;\n}\n\n/**\n * Accept-Language 协商 —— RFC 4647 lookup(含主语言回退)\n *\n * negotiateLocale('zh-CN,en;q=0.9', { locales: ['zh','en'], defaultLocale: 'en' })\n * → 'zh' ('zh-CN' 命中 'zh')\n *\n * negotiateLocale('en-GB', { locales: ['zh-hans','en-us'], defaultLocale: 'zh-hans' })\n * → 'en-us' ('en-GB' 主语言 'en' 回退到第一个 en-*)\n *\n * negotiateLocale(null, ...) → defaultLocale\n */\nexport function negotiateLocale(\n acceptLanguage: string | null | undefined,\n config: I18nConfig\n): string {\n if (!acceptLanguage) return config.defaultLocale;\n const ranked = acceptLanguage\n .split(',')\n .map(part => {\n const [tag, ...params] = part.trim().split(';');\n const q = params.find(p => p.startsWith('q='));\n return { tag: tag.toLowerCase(), q: q ? Number(q.slice(2)) || 0 : 1 };\n })\n .sort((a, b) => b.q - a.q);\n\n const supported = config.locales.map(l => l.toLowerCase());\n for (const { tag } of ranked) {\n // 1) 完整 code 精确匹配('en-us' === 'en-us')\n const exact = supported.indexOf(tag);\n if (exact >= 0) return config.locales[exact];\n\n const primary = tag.split('-')[0];\n\n // 2) 主语言精确匹配('ja' === 'ja',针对无地区后缀的 locale 如 ja/fr/tr)\n const sub = supported.indexOf(primary);\n if (sub >= 0) return config.locales[sub];\n\n // 3) 主语言回退('en' / 'en-GB' → 第一个 en-*;'zh' / 'zh-CN' → zh-hans)\n // 解决「浏览器只发 en / en-GB,而 locales 是 en-us/en-au」这类地区码不匹配问题。\n const primaryPrefix = supported.findIndex(l => l.startsWith(primary + '-'));\n if (primaryPrefix >= 0) return config.locales[primaryPrefix];\n }\n return config.defaultLocale;\n}\n\n/**\n * 列出某业务 pathname 在所有 locale 下的 URL —— 用于 <link rel=\"alternate\" hreflang>\n *\n * alternates('/about', { locales: ['zh','en'], defaultLocale: 'zh' })\n * → [{ hreflang: 'zh', href: '/about' }, { hreflang: 'en', href: '/en/about' }]\n */\nexport function alternates(\n pathname: string,\n config: I18nConfig\n): Array<{ hreflang: string; href: string }> {\n return config.locales.map(locale => ({\n hreflang: locale,\n href: withLocale(pathname, locale, config),\n }));\n}\n\n/**\n * 从 SiteHooks 的 intl 字段提取 URL 路由配置并填默认值\n *\n * 用途:消除消费者侧 `app.tsx` 里那段「从 siteHooks.intl 抠出 locales/defaultLocale/prefixDefault\n * 再补默认值组装 I18nConfig」的重复样板。\n *\n * // before\n * const intl = siteHooks.intl ?? {};\n * const I18N: I18nConfig = {\n * locales: intl.locales ?? ['en'],\n * defaultLocale: intl.defaultLocale ?? 'en',\n * prefixDefault: intl.prefixDefault,\n * };\n *\n * // after\n * const I18N = resolveI18nConfig(siteHooks.intl);\n *\n * 行为:\n * - 缺省 → `{ locales: ['en'], defaultLocale: 'en' }`(单语言英文兜底)\n * - locales 给了但 defaultLocale 没给 → 取 locales[0]\n * - 多余字段(endpoint / load / ttl / detect)忽略,只挑 URL 路由相关\n */\nexport function resolveI18nConfig(\n intl?: {\n locales?: readonly string[];\n defaultLocale?: string;\n prefixDefault?: boolean;\n } | null\n): I18nConfig {\n const locales = intl?.locales && intl.locales.length > 0 ? intl.locales : ['en'];\n const defaultLocale = intl?.defaultLocale ?? locales[0];\n return {\n locales,\n defaultLocale,\n prefixDefault: intl?.prefixDefault,\n };\n}\n"],"mappings":";AAQA,SAAgB,gBAAgB,QAAoC;CAClE,IA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