Fix cross-origin locale imports and release viewer 2.1.1

This commit is contained in:
coyotte508 2026-11-19 00:54:53 +00:00
commit e224cf7c78
4 changed files with 24 additions and 2 deletions

View file

@ -9,3 +9,5 @@ BGS compresses JS, CSS, JSON and WASM automatically during upload. Send the orig
The BGS admin's **Upload folder** control handles multi-file builds. For scripted releases, use BGS's `scripts/publish-viewer.mjs` with `--dir`, `--entry` and `--style`; it verifies every uploaded file, records the previous viewer, and activates only with `--apply --activate`. The full command and API contract are in the [BGS viewer documentation](https://docs.boardgamers.space/guide/viewer-api#publishing-a-viewer-with-multiple-files).
English needs no language download; other languages load when selected. Test a cold load and a language change after publishing. Also check the browser Network panel for missing relative assets.
The UMD build captures its script URL during evaluation and resolves locale imports to absolute URLs. This is required for cross-origin classic scripts: browsers can otherwise give `import()` an `about:blank` base. The tutorial browser suite exercises all 17 languages with HTML and viewer assets on different origins, without requiring the host to set a `crossorigin` attribute.

View file

@ -1,6 +1,6 @@
{
"name": "container-viewer",
"version": "2.1.0",
"version": "2.1.1",
"repository": {
"type": "git",
"url": "git+https://github.com/boardgamers/container.git",

View file

@ -28,6 +28,7 @@ for (const locale of locales.filter((locale) => locale !== 'en')) {
const html =
'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/bundle.css"><div id="app"></div><script src="/vue.js"></script><script src="/bundle.js"></script><script>container.launchTutorial("#app",{chapter:new URLSearchParams(location.search).get("chapter")||"supply-chain",locale:new URLSearchParams(location.search).get("locale")||"en"});</script>';
const server = createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
const request = new URL(req.url, 'http://localhost');
const path = request.pathname;
if (request.searchParams.has('embedded')) {
@ -58,6 +59,18 @@ const server = createServer(async (req, res) => {
});
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const origin = `http://127.0.0.1:${server.address().port}`;
// Model BGS's cross-origin CDN classic script, without a crossorigin attribute.
const crossOriginHost = createServer((req, res) => {
if (req.url === '/favicon.ico') {
res.writeHead(204);
res.end();
return;
}
res.setHeader('Content-Type', 'text/html');
res.end(html.replace(/(src|href)="\//g, `$1="${origin}/`));
});
await new Promise((resolve) => crossOriginHost.listen(0, '127.0.0.1', resolve));
const tutorialHost = `http://127.0.0.1:${crossOriginHost.address().port}`;
async function playOnBoard(page, move) {
if (move.name === 'callBank') {
await page.locator(`.offshore-lot[data-type="${move.data.type}"][data-lot="${move.data.lot}"]`).click();
@ -188,7 +201,7 @@ async function localizedOffshoreSmoke(browser) {
errors.push(`${response.status()}: ${response.url()}`);
});
try {
await page.goto(`${origin}/?chapter=${lesson.id}&locale=${encodeURIComponent(locale)}`);
await page.goto(`${tutorialHost}/?chapter=${lesson.id}&locale=${encodeURIComponent(locale)}`);
await page.waitForFunction(
({ title, instruction, locale }) =>
document.querySelector('.container-tutorial')?.lang === locale &&
@ -410,5 +423,6 @@ try {
}
} finally {
await browser.close();
await new Promise((resolve) => crossOriginHost.close(resolve));
await new Promise((resolve) => server.close(resolve));
}

View file

@ -37,6 +37,11 @@ export default defineConfig(({ mode }) => {
{
name: 'container-locale-sidecars',
apply: 'build',
renderDynamicImport({ targetModuleId }) {
if (!library || !targetModuleId || !localeForId(targetModuleId)) return;
// Classic scripts loaded across origins cannot resolve relative import().
return { left: 'import(new URL(', right: ', __containerViewerUrl).href)' };
},
generateBundle() {
if (!library) return;
for (const locale of locales) {
@ -94,6 +99,7 @@ export default defineConfig(({ mode }) => {
output: library
? {
exports: 'named',
intro: 'const __containerViewerUrl = typeof document !== "undefined" ? (document.currentScript?.src || document.baseURI) : "";',
globals: { vue: 'Vue' },
paths: (id) => {
const locale = localeForId(id);