feat: i18n PWA webmanifests (#805)
Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
parent
18056038c7
commit
85ac005570
7 changed files with 190 additions and 73 deletions
85
modules/pwa/i18n.ts
Normal file
85
modules/pwa/i18n.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import { readFile } from 'fs-extra'
|
||||
import { resolve } from 'pathe'
|
||||
import type { ManifestOptions } from 'vite-plugin-pwa'
|
||||
import { getEnv } from '../../config/env'
|
||||
import { i18n } from '../../config/i18n'
|
||||
import type { LocaleObject } from '#i18n'
|
||||
|
||||
export type LocalizedWebManifest = Record<string, Partial<ManifestOptions>>
|
||||
|
||||
export const pwaLocales = i18n.locales as LocaleObject[]
|
||||
|
||||
type WebManifestEntry = Pick<ManifestOptions, 'name' | 'short_name' | 'description'>
|
||||
type RequiredWebManifestEntry = Required<WebManifestEntry & Pick<ManifestOptions, 'dir' | 'lang'>>
|
||||
|
||||
export const createI18n = async (): Promise<LocalizedWebManifest> => {
|
||||
const { env } = await getEnv()
|
||||
const envName = `${env === 'release' ? '' : `(${env})`}`
|
||||
const { pwa } = await readI18nFile('en-US.json')
|
||||
|
||||
const defaultManifest: Required<WebManifestEntry> = pwa.webmanifest[env]
|
||||
|
||||
const locales: RequiredWebManifestEntry[] = await Promise.all(
|
||||
pwaLocales
|
||||
.filter(l => l.code !== 'en-US')
|
||||
.map(async ({ code, dir = 'ltr', file }) => {
|
||||
// read locale file
|
||||
const { pwa, app_name, app_desc_short } = await readI18nFile(file!)
|
||||
const entry: WebManifestEntry = pwa?.webmanifest?.[env] ?? {}
|
||||
if (!entry.name && app_name)
|
||||
entry.name = dir === 'rtl' ? `${envName} ${app_name}` : `${app_name} ${envName}`
|
||||
|
||||
if (!entry.short_name && app_name)
|
||||
entry.short_name = dir === 'rtl' ? `${envName} ${app_name}` : `${app_name} ${envName}`
|
||||
|
||||
if (!entry.description && app_desc_short)
|
||||
entry.description = app_desc_short
|
||||
|
||||
return <RequiredWebManifestEntry>{
|
||||
...defaultManifest,
|
||||
...entry,
|
||||
lang: code,
|
||||
dir,
|
||||
}
|
||||
}),
|
||||
)
|
||||
locales.push({
|
||||
...defaultManifest,
|
||||
lang: 'en-US',
|
||||
dir: 'ltr',
|
||||
})
|
||||
return locales.reduce((acc, { lang, dir, name, short_name, description }) => {
|
||||
acc[lang] = {
|
||||
scope: '/',
|
||||
id: '/',
|
||||
start_url: '/',
|
||||
display: 'standalone',
|
||||
lang,
|
||||
name,
|
||||
short_name,
|
||||
description,
|
||||
dir,
|
||||
theme_color: '#ffffff',
|
||||
icons: [
|
||||
{
|
||||
src: 'pwa-192x192.png',
|
||||
sizes: '192x192',
|
||||
type: 'image/png',
|
||||
},
|
||||
{
|
||||
src: 'pwa-512x512.png',
|
||||
sizes: '512x512',
|
||||
type: 'image/png',
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {} as LocalizedWebManifest)
|
||||
}
|
||||
|
||||
async function readI18nFile(file: string) {
|
||||
return JSON.parse(Buffer.from(
|
||||
await readFile(resolve(`./locales/${file}`), 'utf-8'),
|
||||
).toString())
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
import { defineNuxtModule } from '@nuxt/kit'
|
||||
import type { VitePWAOptions, VitePluginPWAAPI } from 'vite-plugin-pwa'
|
||||
import type { VitePluginPWAAPI } from 'vite-plugin-pwa'
|
||||
import { VitePWA } from 'vite-plugin-pwa'
|
||||
import type { Plugin } from 'vite'
|
||||
import type { VitePWANuxtOptions } from './types'
|
||||
import { configurePWAOptions } from './config'
|
||||
import { type LocalizedWebManifest, createI18n, pwaLocales } from './i18n'
|
||||
|
||||
export * from './types'
|
||||
export default defineNuxtModule<VitePWANuxtOptions>({
|
||||
|
@ -20,6 +21,7 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
|||
const resolveVitePluginPWAAPI = (): VitePluginPWAAPI | undefined => {
|
||||
return vitePwaClientPlugin?.api
|
||||
}
|
||||
let webmanifests: LocalizedWebManifest | undefined
|
||||
|
||||
// TODO: combine with configurePWAOptions?
|
||||
nuxt.hook('nitro:init', (nitro) => {
|
||||
|
@ -37,12 +39,55 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
|||
const plugin = viteInlineConfig.plugins.find(p => p && typeof p === 'object' && 'name' in p && p.name === 'vite-plugin-pwa')
|
||||
if (plugin)
|
||||
throw new Error('Remove vite-plugin-pwa plugin from Vite Plugins entry in Nuxt config file!')
|
||||
const resolvedOptions: Partial<VitePWAOptions> = {
|
||||
...options,
|
||||
manifest: options.manifest ? await options.manifest() : undefined,
|
||||
|
||||
webmanifests = await createI18n()
|
||||
const generateManifest = (locale: string) => {
|
||||
const manifest = webmanifests![locale]
|
||||
if (!manifest)
|
||||
throw new Error(`No webmanifest found for locale ${locale}`)
|
||||
return JSON.stringify(manifest)
|
||||
}
|
||||
configurePWAOptions(resolvedOptions, nuxt)
|
||||
const plugins = VitePWA(resolvedOptions)
|
||||
viteInlineConfig.plugins.push({
|
||||
name: 'elk:pwa:locales:build',
|
||||
apply: 'build',
|
||||
generateBundle(_, bundle) {
|
||||
if (options.disable || !bundle)
|
||||
return
|
||||
|
||||
Object.keys(webmanifests!).map(l => [l, `manifest-${l}.webmanifest`]).forEach(([l, fileName]) => {
|
||||
bundle[fileName] = {
|
||||
isAsset: true,
|
||||
type: 'asset',
|
||||
name: undefined,
|
||||
source: generateManifest(l),
|
||||
fileName,
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
viteInlineConfig.plugins.push({
|
||||
name: 'elk:pwa:locales:dev',
|
||||
apply: 'serve',
|
||||
configureServer(server) {
|
||||
const localeMatcher = new RegExp(`^${nuxt.options.app.baseURL}manifest-(.*).webmanifest$`)
|
||||
server.middlewares.use((req, res, next) => {
|
||||
const match = req.url?.match(localeMatcher)
|
||||
const entry = match && webmanifests![match[1]]
|
||||
if (entry) {
|
||||
res.statusCode = 200
|
||||
res.setHeader('Content-Type', 'application/manifest+json')
|
||||
res.write(JSON.stringify(entry), 'utf-8')
|
||||
res.end()
|
||||
}
|
||||
else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
configurePWAOptions(options, nuxt)
|
||||
const plugins = VitePWA(options)
|
||||
viteInlineConfig.plugins.push(plugins)
|
||||
if (isClient)
|
||||
vitePwaClientPlugin = plugins.find(p => p.name === 'vite-plugin-pwa') as Plugin
|
||||
|
@ -61,8 +106,17 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
|||
return
|
||||
|
||||
viteServer.middlewares.stack.push({ route: webManifest, handle: emptyHandle })
|
||||
if (webmanifests) {
|
||||
Object.keys(webmanifests).forEach((locale) => {
|
||||
viteServer.middlewares.stack.push({
|
||||
route: `${nuxt.options.app.baseURL}manifest-${locale}.webmanifest`,
|
||||
handle: emptyHandle,
|
||||
})
|
||||
})
|
||||
}
|
||||
viteServer.middlewares.stack.push({ route: devSw, handle: emptyHandle })
|
||||
})
|
||||
|
||||
if (!options.strategies || options.strategies === 'generateSW') {
|
||||
nuxt.hook('vite:serverCreated', (viteServer, { isServer }) => {
|
||||
if (isServer)
|
||||
|
@ -76,6 +130,16 @@ export default defineNuxtModule<VitePWANuxtOptions>({
|
|||
}
|
||||
}
|
||||
else {
|
||||
nuxt.hook('nitro:config', async (nitroConfig) => {
|
||||
nitroConfig.routeRules = nitroConfig.routeRules || {}
|
||||
for (const locale of pwaLocales) {
|
||||
nitroConfig.routeRules![`/manifest-${locale.code}.webmanifest`] = {
|
||||
headers: {
|
||||
'Content-Type': 'application/manifest+json',
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
nuxt.hook('nitro:init', (nitro) => {
|
||||
nitro.hooks.hook('rollup:before', async () => {
|
||||
await resolveVitePluginPWAAPI()?.generateSW()
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
import type { ManifestOptions, VitePWAOptions } from 'vite-plugin-pwa'
|
||||
import type { Overwrite } from '../../types/utils'
|
||||
import type { VitePWAOptions } from 'vite-plugin-pwa'
|
||||
|
||||
export type VitePWANuxtOptions = Overwrite<Partial<VitePWAOptions>, {
|
||||
manifest?: () => Promise<Partial<ManifestOptions>>
|
||||
}>
|
||||
export interface VitePWANuxtOptions extends Partial<VitePWAOptions> {}
|
||||
|
||||
declare module '@nuxt/schema' {
|
||||
interface NuxtConfig {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue