refactor: move pwa/build-info -> appConfig (#1508)

This commit is contained in:
Daniel Roe 2023-01-29 06:18:27 -08:00 committed by GitHub
parent 1c9004a731
commit 415d36ce32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 27 additions and 18 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
const buildInfo = useRuntimeConfig().public.buildInfo const buildInfo = useAppConfig().buildInfo
const timeAgoOptions = useTimeAgoOptions() const timeAgoOptions = useTimeAgoOptions()
const userSettings = useUserSettings() const userSettings = useUserSettings()

View file

@ -17,7 +17,7 @@ const {
} = usePushManager() } = usePushManager()
const { t } = useI18n() const { t } = useI18n()
const pwaEnabled = useRuntimeConfig().public.pwaEnabled const pwaEnabled = useAppConfig().pwaEnabled
let busy = $ref<boolean>(false) let busy = $ref<boolean>(false)
let animateSave = $ref<boolean>(false) let animateSave = $ref<boolean>(false)

View file

@ -35,5 +35,5 @@ export const teams: Team[] = [
].sort(() => Math.random() - 0.5) ].sort(() => Math.random() - 0.5)
export function useBuildInfo() { export function useBuildInfo() {
return useRuntimeConfig().public.buildInfo as BuildInfo return useAppConfig().buildInfo as BuildInfo
} }

View file

@ -43,7 +43,7 @@ export function setupPageHeader() {
return titleTemplate return titleTemplate
}, },
link: process.client && useRuntimeConfig().public.pwaEnabled link: process.client && useAppConfig().pwaEnabled
? () => [{ ? () => [{
key: 'webmanifest', key: 'webmanifest',
rel: 'manifest', rel: 'manifest',

View file

@ -149,7 +149,7 @@ export async function loginTo(masto: ElkMasto, user: Overwrite<UserLogin, { acco
const [me, pushSubscription] = await Promise.all([ const [me, pushSubscription] = await Promise.all([
fetchAccountInfo(client, user.server), fetchAccountInfo(client, user.server),
// if PWA is not enabled, don't get push subscription // if PWA is not enabled, don't get push subscription
useRuntimeConfig().public.pwaEnabled useAppConfig().pwaEnabled
// we get 404 response instead empty data // we get 404 response instead empty data
? client.v1.webPushSubscriptions.fetch().catch(() => Promise.resolve(undefined)) ? client.v1.webPushSubscriptions.fetch().catch(() => Promise.resolve(undefined))
: Promise.resolve(undefined), : Promise.resolve(undefined),
@ -194,7 +194,7 @@ export async function removePushNotificationData(user: UserLogin, fromSWPushMana
// clear push notification policy // clear push notification policy
delete useLocalStorage<PushNotificationPolicy>(STORAGE_KEY_NOTIFICATION_POLICY, {}).value[acct] delete useLocalStorage<PushNotificationPolicy>(STORAGE_KEY_NOTIFICATION_POLICY, {}).value[acct]
const pwaEnabled = useRuntimeConfig().public.pwaEnabled const pwaEnabled = useAppConfig().pwaEnabled
const pwa = useNuxtApp().$pwa const pwa = useNuxtApp().$pwa
const registrationError = pwa?.registrationError === true const registrationError = pwa?.registrationError === true
const unregister = pwaEnabled && !registrationError && pwa?.registrationError === true && fromSWPushManager const unregister = pwaEnabled && !registrationError && pwa?.registrationError === true && fromSWPushManager

View file

@ -19,8 +19,12 @@ export default defineNuxtModule({
env, env,
} }
nuxt.options.runtimeConfig.public.env = env nuxt.options.appConfig = nuxt.options.appConfig || {}
nuxt.options.runtimeConfig.public.buildInfo = buildInfo nuxt.options.appConfig.env = env
nuxt.options.appConfig.buildInfo = buildInfo
nuxt.options.nitro.virtual = nuxt.options.nitro.virtual || {}
nuxt.options.nitro.virtual['#build-info'] = `export const env = ${JSON.stringify(env)}`
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || [] nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || []
if (env === 'dev') if (env === 'dev')

View file

@ -23,6 +23,9 @@ export default defineNuxtModule<VitePWANuxtOptions>({
} }
let webmanifests: LocalizedWebManifest | undefined let webmanifests: LocalizedWebManifest | undefined
nuxt.options.appConfig = nuxt.options.appConfig || {}
nuxt.options.appConfig.pwaEnabled = !options.disable
// TODO: combine with configurePWAOptions? // TODO: combine with configurePWAOptions?
nuxt.hook('nitro:init', (nitro) => { nuxt.hook('nitro:init', (nitro) => {
options.outDir = nitro.options.output.publicDir options.outDir = nitro.options.output.publicDir

View file

@ -23,6 +23,7 @@ export default defineNuxtModule({
'unstorage/drivers/fs': 'unenv/runtime/mock/proxy', 'unstorage/drivers/fs': 'unenv/runtime/mock/proxy',
'unstorage/drivers/cloudflare-kv-http': 'unenv/runtime/mock/proxy', 'unstorage/drivers/cloudflare-kv-http': 'unenv/runtime/mock/proxy',
'node:events': 'unenv/runtime/node/events/index', 'node:events': 'unenv/runtime/node/events/index',
'#build-info': resolve('./runtime/build-info'),
} }
nuxt.hook('vite:extend', ({ config }) => { nuxt.hook('vite:extend', ({ config }) => {

View file

@ -0,0 +1 @@
export const env = useAppConfig().env

View file

@ -4,7 +4,6 @@ import { isCI, isDevelopment, isWindows } from 'std-env'
import { isPreview } from './config/env' import { isPreview } from './config/env'
import { i18n } from './config/i18n' import { i18n } from './config/i18n'
import { pwa } from './config/pwa' import { pwa } from './config/pwa'
import type { BuildInfo } from './types'
const { resolve } = createResolver(import.meta.url) const { resolve } = createResolver(import.meta.url)
@ -96,10 +95,8 @@ export default defineNuxtConfig({
}, },
public: { public: {
privacyPolicyUrl: '', privacyPolicyUrl: '',
env: '', // set in build-env module // We use LibreTranslate (https://github.com/LibreTranslate/LibreTranslate) as
buildInfo: {} as BuildInfo, // set in build-env module // our default translation server #76
pwaEnabled: !isDevelopment || process.env.VITE_DEV_PWA === 'true',
// We use LibreTranslate(https://github.com/LibreTranslate/LibreTranslate) as our default translation server #76
translateApi: '', translateApi: '',
// Use the instance where Elk has its Mastodon account as the default // Use the instance where Elk has its Mastodon account as the default
defaultServer: 'm.webtoo.ls', defaultServer: 'm.webtoo.ls',

View file

@ -6,7 +6,7 @@ definePageMeta({
}) })
const { t } = useI18n() const { t } = useI18n()
const pwaEnabled = useRuntimeConfig().public.pwaEnabled const pwaEnabled = useAppConfig().pwaEnabled
const tabs = $computed<CommonRouteTabOption[]>(() => [ const tabs = $computed<CommonRouteTabOption[]>(() => [
{ {

View file

@ -4,7 +4,7 @@ definePageMeta({
}) })
const { t } = useI18n() const { t } = useI18n()
const pwaEnabled = useRuntimeConfig().public.pwaEnabled const pwaEnabled = useAppConfig().pwaEnabled
useHeadFixed({ useHeadFixed({
title: () => `${t('settings.notifications.label')} | ${t('nav.settings')}`, title: () => `${t('settings.notifications.label')} | ${t('nav.settings')}`,

View file

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
definePageMeta({ definePageMeta({
middleware: ['auth', () => { middleware: ['auth', () => {
if (!useRuntimeConfig().public.pwaEnabled) if (!useAppConfig().pwaEnabled)
return navigateTo('/settings/notifications') return navigateTo('/settings/notifications')
}], }],
}) })

View file

@ -1,7 +1,7 @@
<script setup> <script setup>
definePageMeta({ definePageMeta({
middleware: () => { middleware: () => {
if (!useRuntimeConfig().public.pwaEnabled) if (!useAppConfig().pwaEnabled)
return navigateTo('/') return navigateTo('/')
}, },
}) })

View file

@ -12,6 +12,9 @@ import type { Storage } from 'unstorage'
import cached from './cache-driver' import cached from './cache-driver'
// @ts-expect-error virtual import
import { env } from '#build-info'
import type { AppInfo } from '~/types' import type { AppInfo } from '~/types'
import { APP_NAME } from '~/constants' import { APP_NAME } from '~/constants'
@ -45,7 +48,7 @@ async function fetchAppInfo(origin: string, server: string) {
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, { const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
method: 'POST', method: 'POST',
body: { body: {
client_name: APP_NAME + (config.public.env !== 'release' ? ` (${config.public.env})` : ''), client_name: APP_NAME + (env !== 'release' ? ` (${env})` : ''),
website: 'https://elk.zone', website: 'https://elk.zone',
redirect_uris: getRedirectURI(origin, server), redirect_uris: getRedirectURI(origin, server),
scopes: 'read write follow push', scopes: 'read write follow push',