Просмотр исходного кода

Apply the workshop's forced language on the portal and share pages (#602)

The language for customer pages was read from a request header that nothing
sets, so a workshop that forces its own language on customer-facing pages
still got the visitor's browser language there, and staff notices could show
on customer pages. The path now comes from the header the proxy really sets.
Bernt Christian Egeland 10 часов назад
Родитель
Сommit
88021f600b
4 измененных файлов с 58 добавлено и 2 удалено
  1. 40 0
      src/__tests__/lib/request-path.test.ts
  2. 2 1
      src/app/layout.tsx
  3. 2 1
      src/i18n/request.ts
  4. 14 0
      src/lib/request-path.ts

+ 40 - 0
src/__tests__/lib/request-path.test.ts

@@ -0,0 +1,40 @@
+import { readFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { describe, expect, it } from 'vitest'
+import { extractCustomerOrgParam } from '@/i18n/locale-from-request'
+import { REQUEST_PATH_HEADER, requestPathname } from '@/lib/request-path'
+
+const headersOf = (value: string | null) => ({
+  get: (name: string) => (name === REQUEST_PATH_HEADER ? value : null),
+})
+
+describe('requestPathname', () => {
+  it('gives the path the proxy recorded, without its query', () => {
+    expect(requestPathname(headersOf('/portal/fjordside/dashboard?error=expired'))).toBe(
+      '/portal/fjordside/dashboard'
+    )
+  })
+
+  it('is null when no page request is being served', () => {
+    expect(requestPathname(headersOf(null))).toBeNull()
+  })
+
+  it('lets a customer page be recognised, so the workshop language can be forced on it', () => {
+    const path = requestPathname(headersOf('/share/invoice/fjordside/tok123?x=1'))
+    expect(extractCustomerOrgParam(path)).toBe('fjordside')
+  })
+})
+
+describe('who reads the request path', () => {
+  // The language resolver and the root layout once read "x-pathname", a
+  // header nothing sets, so customer pages ignored a forced workshop language
+  // and showed staff notices.
+  it.each([
+    'src/i18n/request.ts',
+    'src/app/layout.tsx',
+  ])('%s goes through requestPathname', (file) => {
+    const source = readFileSync(join(process.cwd(), file), 'utf8')
+    expect(source).toContain('requestPathname(')
+    expect(source).not.toContain('x-pathname')
+  })
+})

+ 2 - 1
src/app/layout.tsx

@@ -1,3 +1,4 @@
+import { requestPathname } from '@/lib/request-path'
 import type { Metadata } from 'next'
 import { NextIntlClientProvider } from 'next-intl'
 import { getLocale, getMessages } from 'next-intl/server'
@@ -129,7 +130,7 @@ export default async function RootLayout({
   // workshop's branding, not ours. A white-label licence exists precisely so
   // Torqvoice does not appear on that paperwork, and a platform notice there
   // would be both off-brand and none of the customer's business.
-  const pathname = (await headers()).get('x-pathname')
+  const pathname = requestPathname(await headers())
   const broadcast = isCustomerFacingPath(pathname) ? null : await getBroadcast()
   // A custom icon is declared through generateMetadata, apple icon included.
   const branding = await getAppBranding()

+ 2 - 1
src/i18n/request.ts

@@ -7,11 +7,12 @@ import {
   resolveCustomerLocale,
 } from './locale-from-request'
 import { resolvePortalOrg } from '@/lib/portal-slug'
+import { requestPathname } from '@/lib/request-path'
 
 export default getRequestConfig(async () => {
   const headerStore = await headers()
   const acceptLanguage = headerStore.get('accept-language')
-  const pathname = headerStore.get('x-pathname')
+  const pathname = requestPathname(headerStore)
 
   let locale: Locale
 

+ 14 - 0
src/lib/request-path.ts

@@ -4,3 +4,17 @@
  * readers still pass it through safeRedirectPath.
  */
 export const REQUEST_PATH_HEADER = 'x-torqvoice-path'
+
+/**
+ * The path a page request asked for, without its query: what a layout or the
+ * language resolver matches routes against. Null outside a page request.
+ *
+ * Read the header through this, never by a name of your own: the portal and
+ * the share pages once looked for a header nothing set, so a workshop that
+ * forced its language on customers got the visitor's instead.
+ */
+export function requestPathname(headerStore: { get(name: string): string | null }): string | null {
+  const requested = headerStore.get(REQUEST_PATH_HEADER)
+  if (!requested) return null
+  return requested.split('?')[0] || null
+}