1212 * zh-CN→zh_CN, zh-TW→zh_TW) are handled via XLF_LOCALE_MAP.
1313 */
1414
15+ import { createHash } from 'crypto'
1516import { existsSync , readFileSync , writeFileSync } from 'fs'
1617import {
1718 makeEs2015TranslatePlugin ,
@@ -36,6 +37,22 @@ const XLF_LOCALE_MAP: Record<string, string> = {
3637/** Source file (pre-translation). */
3738const PRINT_VIEW_SOURCE = './src/assets/print-view/fetch-orcid.js'
3839
40+ /**
41+ * Returns an 8-character hash of the built print-view assets for a locale,
42+ * used as a cache-busting query param: `?v=<hash>-<locale>`.
43+ * Hashes the translated JS + the shared CSS so any change invalidates the cache.
44+ */
45+ function printViewCacheKey ( destDir : string , locale : string ) : string {
46+ const hash = createHash ( 'sha256' )
47+ for ( const file of [ 'fetch-orcid.js' , 'cv-style.css' ] ) {
48+ const filePath = `${ destDir } /${ file } `
49+ if ( existsSync ( filePath ) ) {
50+ hash . update ( readFileSync ( filePath ) )
51+ }
52+ }
53+ return `${ hash . digest ( 'hex' ) . slice ( 0 , 8 ) } -${ locale } `
54+ }
55+
3956function xlfFileForLocale ( locale : string ) : string {
4057 const suffix = XLF_LOCALE_MAP [ locale ] ?? locale
4158 return `./src/locale/messages.${ suffix } .xlf`
@@ -132,19 +149,22 @@ export function localizeAndWritePrintViewScript(): void {
132149 writeFileSync ( destFile , output , 'utf8' )
133150 console . log ( `[print-view-localize] ✓ ${ destFile } ` )
134151
135- // Fix the lang attribute in the copied print-view/index.html.
136- // The source file always has lang="en"; we rewrite it to the actual locale .
152+ // Fix the lang attribute and cache-busting version in print-view/index.html.
153+ // The source file always has lang="en" and a static ?v=...; we rewrite both .
137154 const printViewIndexPath = `${ destDir } /index.html`
138155 if ( existsSync ( printViewIndexPath ) ) {
139- const htmlSource = readFileSync ( printViewIndexPath , 'utf8' )
140- const htmlLocalized = htmlSource . replace (
156+ const cacheKey = printViewCacheKey ( destDir , locale )
157+ let html = readFileSync ( printViewIndexPath , 'utf8' )
158+ const htmlOriginal = html
159+ html = html . replace (
141160 / < h t m l ( [ ^ > ] * ) l a n g = " [ ^ " ] * " / ,
142161 `<html$1lang="${ locale } "`
143162 )
144- if ( htmlLocalized !== htmlSource ) {
145- writeFileSync ( printViewIndexPath , htmlLocalized , 'utf8' )
163+ html = html . replace ( / _ _ P R I N T _ V I E W _ V E R S I O N _ _ / g, cacheKey )
164+ if ( html !== htmlOriginal ) {
165+ writeFileSync ( printViewIndexPath , html , 'utf8' )
146166 console . log (
147- `[print-view-localize] ✓ ${ printViewIndexPath } (lang=${ locale } )`
167+ `[print-view-localize] ✓ ${ printViewIndexPath } (lang=${ locale } , v= ${ cacheKey } )`
148168 )
149169 }
150170 }
0 commit comments