@museumwnf/viewer-core 2.26.0 → 2.27.0

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 CHANGED
@@ -157,6 +157,7 @@ What a website's `main.js` still writes, and why it cannot move here:
157
157
  | `media` | `{ legacyHost }` | no | the host of the legacy media server, for [`mediaUrl()`](#the-declaration-outside-a-component) |
158
158
  | `links` | object | no | the addresses a website links out to: `portal`, `galleries`, `myCollection`, `about`, `contact`, `legalNotice`, `credits`, `cookies` |
159
159
  | `site.origin` | string | no | the absolute origin this build is deployed at, base path included when there is one (`https://metanull.github.io/islamicart`, no trailing slash) — declared once here rather than baked into the package, since the package cannot know where it will be served; read by [`sourceUrl()`](#the-declaration-outside-a-component) |
160
+ | `site.legacyHost` | string | no | the legacy host this site replaces (`books.museumwnf.org`): once that host points at the site, an old address on it (`/book/5/en`) opens the page it names (see [Old addresses](#old-addresses-at-the-domain-switch)); nothing without it |
160
161
  | `site.slug` | string | no | the site's slug, as in the exporters' instance files; defaults to the last segment of `site.origin`. The global search's link sends it as `from` |
161
162
  | `globalSearch` | `true` or `{ url }` | no | **off by default.** Turns on the link to the global search (inventory-app M15, ADR 005): `true` for its default address (`GLOBAL_SEARCH_URL`), `{ url }` for another https address. viewer-layout's header then shows its search box. The parity check turns it on for the sites whose legacy had a similar feature |
162
163
 
@@ -485,6 +486,7 @@ under the main partner it belongs to.
485
486
  | `useSection()` | the `meta.section` of the current route — see Routing |
486
487
  | `useFeaturedRecord(entity, { withImage = true, seed, filter })` | one record at random for a landing page's spotlight, among those with an image; null until the entity is loaded; `seed` pins the pick; `filter(record)` applies a site's own visible rule first |
487
488
  | `sectionMeta(chrome = [])` | returns `meta(section, ...entities) => ({ section, entities: [...chrome, ...entities] })` — a route's `meta` in one call, `chrome` being the entities every page of the site loads regardless of which one it is |
489
+ | `mwnfPortalPages` | the portal's own pages a site's footer links to (`chronology`, `support`, `volunteers`, `honoraryCommittee`, `commemoration`, `pressLounge`, `languagePolicy`, `leagueOfArabStates`, `cafeteria`, `partnerList`) — frozen, kept apart from `mwnfLinks` so the DXA configs' `links` do not change |
488
490
  | `mwnfLinks` | the portal and sibling-site addresses the four DXA configs each repeat under `links` — frozen, spread into a site's own `links` rather than retyped |
489
491
 
490
492
  #### Projects, from the data package (epic metanull/inventory-app#1727)
@@ -708,6 +710,9 @@ The convention, applied by the router every website shares:
708
710
  location (`{ name, params }` or a path) or nothing, which is the not-found
709
711
  page. It may be async, so a website can load the entity it maps a legacy
710
712
  key through `backward_compatibility` with;
713
+ - an old address of the legacy site this one replaces, once its host points
714
+ here, is a path the hash router never sees: see
715
+ [Old addresses](#old-addresses-at-the-domain-switch);
711
716
  - a route says what section it belongs to — `meta: { section: 'collection' }`
712
717
  — and a shell reads it with `useSection()` for the banner title or the
713
718
  active menu entry, instead of deriving it from the path;
@@ -736,6 +741,23 @@ export default {
736
741
  }
737
742
  ```
738
743
 
744
+ ### Old addresses at the domain switch
745
+
746
+ The router uses hash history, so an old address of the legacy site
747
+ (`https://books.museumwnf.org/book/5/en`) is a path it never sees, and a
748
+ static host has no file for it. Once the legacy host points at the new site:
749
+
750
+ 1. the build adds a `404.html` that is the site itself:
751
+ `defineViewerConfig({ ..., notFoundPage: true })`;
752
+ 2. the site declares the host it replaces: `site.legacyHost`;
753
+ 3. before the router starts, `createViewer` calls
754
+ `openLegacyAddress(config.site)`: the address becomes
755
+ `<base>#<route>`, the route `legacyAddressRoute()` gives (the same
756
+ table `toNewSite()` rewrites links with), or the not-found page.
757
+
758
+ `legacyAddressRoute(href)` answers the new site's route of any legacy MWNF
759
+ address, or null. A site without `site.legacyHost` is unchanged.
760
+
739
761
  ### Views
740
762
 
741
763
  Every website has three slots — the home page, a list page, a record page —
@@ -808,7 +830,7 @@ The package exports shared smoke-test helpers at @museumwnf/viewer-core/testing:
808
830
  | checkRoutes(config, { names, legacyPaths }) | Verify every route has a name, no route is a catch-all, and expected names and legacy paths are present. Returns the list of problems found. |
809
831
  | checkSectionMeta(config) | Verify every route has a meta.section string (used by the menu to highlight the current page). Returns the list of problems found. |
810
832
  | checkTextsRendered(host, { namespaces }) | Match text keys in the rendered host against a regex like /\b(ns1\|ns2)\.[a-z]/i. Returns the list of namespaces found (empty if none). |
811
- | defineViewerConfig({ dataPackage, inline, plugins, root }) | Return the Vite config object every website shares: the @inventory-data alias, optimizeDeps to inline viewer packages, testTimeout of 60s, and the test environment. Exported from `@museumwnf/viewer-core/vite` for use in `vite.config.js` (separate entry because Vite's config loader runs under Node.js, which cannot parse `.vue` files; the `/testing` barrel reaches a `.vue` file). dataPackage is the npm package name; inline is an optional array of extra packages; plugins is the Vite plugins array (e.g. @vitejs/plugin-vue); root is the project root path (default `process.cwd()`), used to resolve the data package from the project root. |
833
+ | defineViewerConfig({ dataPackage, inline, plugins, notFoundPage, root }) | Return the Vite config object every website shares: the @inventory-data alias, optimizeDeps to inline viewer packages, testTimeout of 60s, and the test environment. Exported from `@museumwnf/viewer-core/vite` for use in `vite.config.js` (separate entry because Vite's config loader runs under Node.js, which cannot parse `.vue` files; the `/testing` barrel reaches a `.vue` file). dataPackage is the npm package name; inline is an optional array of extra packages; plugins is the Vite plugins array (e.g. @vitejs/plugin-vue); root is the project root path (default `process.cwd()`), used to resolve the data package from the project root. notFoundPage (default false) adds the build's `404.html`, a copy of its `index.html`, for [old addresses](#old-addresses-at-the-domain-switch). |
812
834
  | checkOfferedLanguages(config) | Check that the website offers only languages with content, in the declared order, with labels on the switcher. Returns the list of problems found. |
813
835
 
814
836
  @museumwnf/viewer-core/testing is not imported by websites, only by their tests. A website test might be:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@museumwnf/viewer-core",
3
- "version": "2.26.0",
3
+ "version": "2.27.0",
4
4
  "description": "Application engine for MWNF websites: turns a dataset.config.js into a mounted Vue application.",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -166,3 +166,22 @@ export const mwnfLinks = Object.freeze({
166
166
  baroqueArt: 'https://baroqueart.museumwnf.org',
167
167
  sharingHistory: 'https://sharinghistory.museumwnf.org',
168
168
  })
169
+
170
+ /**
171
+ * The portal's own pages a site's footer links to (the Publications footer,
172
+ * as legacy's books pages drew it, read from the portal's footer). Kept here
173
+ * so they change in one place when the portal does; apart from `mwnfLinks`,
174
+ * which the DXA configs copy whole into their `links`.
175
+ */
176
+ export const mwnfPortalPages = Object.freeze({
177
+ chronology: 'https://www.museumwnf.org/about/chronology',
178
+ support: 'https://www.museumwnf.org/about/support',
179
+ volunteers: 'https://www.museumwnf.org/about/volunteers',
180
+ honoraryCommittee: 'https://www.museumwnf.org/about/honorary-committee',
181
+ commemoration: 'https://www.museumwnf.org/about/commemoration',
182
+ pressLounge: 'https://www.museumwnf.org/about/press-lounge',
183
+ languagePolicy: 'https://www.museumwnf.org/about/language-policy',
184
+ leagueOfArabStates: 'https://www.museumwnf.org/league-of-arab-states/',
185
+ cafeteria: 'https://www.museumwnf.org/cafeteria/',
186
+ partnerList: 'https://www.museumwnf.org/partner_list.php',
187
+ })
@@ -7,7 +7,7 @@ import { connectLanguageToRouter, resolveInitialLanguage } from './i18n/language
7
7
  import { VIEWER_CONFIG } from './injectionKeys.js'
8
8
  import { createViewerRouter } from './router/index.js'
9
9
  import { setSiteConfig } from './siteConfig.js'
10
- import { watchNewSiteLinks } from './legacySites.js'
10
+ import { openLegacyAddress, watchNewSiteLinks } from './legacySites.js'
11
11
  import './styles/base.css'
12
12
 
13
13
  export function createViewer(config = {}) {
@@ -16,6 +16,9 @@ export function createViewer(config = {}) {
16
16
  const messages = config.messages ?? {}
17
17
  const resolved = { ...config, languages }
18
18
 
19
+ // An old link to the legacy site this one replaces opens the page it
20
+ // names (`site.legacyHost`; nothing without it).
21
+ openLegacyAddress(config.site)
19
22
  const router = createViewerRouter(config)
20
23
  const i18n = createI18n({ messages, locale: resolveInitialLanguage(languages) })
21
24
 
package/src/index.js CHANGED
@@ -20,7 +20,7 @@ export { defaultScrollBehavior, resolveViews } from './router/index.js'
20
20
  export { mediaUrl, useSiteConfig } from './siteConfig.js'
21
21
  export { bindDisplayLanguage, displayLanguage } from './displayLanguage.js'
22
22
  export { siblingSiteUrl, sourceUrl, useSiteRights } from './rights.js'
23
- export { LEGACY_SITES, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
23
+ export { LEGACY_SITES, legacyAddressRoute, openLegacyAddress, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
24
24
  export { GLOBAL_SEARCH_URL, globalSearchUrl, siteSlug, useGlobalSearch } from './globalSearch.js'
25
25
  export { legacyRecordId } from './legacyIds.js'
26
26
 
@@ -53,7 +53,7 @@ export { groupByCountry, partnerHierarchy, partnerView } from './record/partners
53
53
 
54
54
  // Conventions every website had written for itself.
55
55
  export {
56
- mwnfLinks, projectLabel, projectLinks, sectionMeta, useFeaturedRecord,
56
+ mwnfLinks, mwnfPortalPages, projectLabel, projectLinks, sectionMeta, useFeaturedRecord,
57
57
  useProjects, useSection,
58
58
  } from './conventions.js'
59
59
 
@@ -334,3 +334,51 @@ export function toNewSite(href) {
334
334
  if (route === null) return href
335
335
  return siblingSiteUrl(row.slug, route) ?? href
336
336
  }
337
+
338
+ /**
339
+ * The route on the new site that a legacy MWNF address lands on (a book's
340
+ * `/publication/<id>/<language>` for `books.museumwnf.org/book/5/en`), or
341
+ * null when the address is no legacy site's, or names nothing there.
342
+ */
343
+ export function legacyAddressRoute(href) {
344
+ let url
345
+ try {
346
+ url = new URL(String(href ?? '').trim())
347
+ } catch {
348
+ return null
349
+ }
350
+ const row = rowFor(url)
351
+ return row ? routeFor(row, url) : null
352
+ }
353
+
354
+ /**
355
+ * Turns the page's own address into the route it names when it is one of
356
+ * the site's legacy addresses: a site declares the legacy host it replaces
357
+ * (`site.legacyHost`, e.g. `books.museumwnf.org`) and its address
358
+ * (`site.origin`). Once that host points at the site, an old link such as
359
+ * `/book/5/en` reaches the build's 404 page (`defineViewerConfig({
360
+ * notFoundPage: true })`), which is the site itself: before the router
361
+ * starts, the address becomes `<base>#/publication/<id>/en`. An address
362
+ * that names nothing goes to the not-found page. Does nothing without
363
+ * `legacyHost`, on the site's home, or when the address already has a
364
+ * route. Returns the address it moved to, or null.
365
+ */
366
+ export function openLegacyAddress(site, win = typeof window === 'undefined' ? undefined : window) {
367
+ const host = site?.legacyHost
368
+ if (!host || !win?.location || !win.history?.replaceState) return null
369
+ const { pathname, search, hash } = win.location
370
+ if (hash && hash !== '#' && hash !== '#/') return null
371
+ let base = '/'
372
+ try {
373
+ base = new URL(site.origin).pathname.replace(/\/?$/, '/')
374
+ } catch {
375
+ // No origin: the site is served at the root.
376
+ }
377
+ if (!pathname.startsWith(base)) return null
378
+ const rest = `/${pathname.slice(base.length)}`
379
+ if (rest === '/' || rest === '/index.html' || rest === '/404.html') return null
380
+ const route = legacyAddressRoute(`https://${bareHost(host)}${rest}${search}`) ?? rest
381
+ const target = `${base}#${route}`
382
+ win.history.replaceState(win.history.state, '', target)
383
+ return target
384
+ }
@@ -11,6 +11,9 @@ import path from 'node:path'
11
11
  * the required `'@museumwnf/viewer-core'` and `'@museumwnf/viewer-layout'`.
12
12
  * `plugins` is an optional array of Vite plugins (e.g. `@vitejs/plugin-vue`),
13
13
  * which the site must supply because this package does not depend on them.
14
+ * `notFoundPage`, when true, adds the build's `404.html`: a copy of its
15
+ * `index.html`, so GitHub Pages serves the site itself for an address it
16
+ * has no file for, and an old link reaches `openLegacyAddress`.
14
17
  * `root` is an optional project root path (default `process.cwd()`); use it when
15
18
  * loading the config from a context other than the project directory.
16
19
  */
@@ -18,10 +21,11 @@ export function defineViewerConfig({
18
21
  dataPackage,
19
22
  inline = [],
20
23
  plugins = [],
24
+ notFoundPage = false,
21
25
  root = process.cwd(),
22
26
  } = {}) {
23
27
  return {
24
- plugins,
28
+ plugins: notFoundPage ? [...plugins, notFoundPagePlugin()] : plugins,
25
29
  resolve: {
26
30
  alias: {
27
31
  // viewer-core reads every JSON of the data package through this alias.
@@ -73,3 +77,16 @@ export function defineViewerConfig({
73
77
  },
74
78
  }
75
79
  }
80
+
81
+ /** The build's `404.html`, a copy of its `index.html` (`notFoundPage`). */
82
+ export function notFoundPagePlugin() {
83
+ return {
84
+ name: 'viewer-not-found-page',
85
+ apply: 'build',
86
+ enforce: 'post',
87
+ generateBundle(_options, bundle) {
88
+ const index = bundle['index.html']
89
+ if (index?.type === 'asset') this.emitFile({ type: 'asset', fileName: '404.html', source: index.source })
90
+ },
91
+ }
92
+ }