fix(nuxt3): enable legacy access to $config and static flags (#1588)

This commit is contained in:
Daniel Roe 2021-11-02 11:28:19 +00:00 committed by GitHub
parent efe1fea1d1
commit f3ff87c812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 11 deletions

View File

@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'http'
import type { App } from 'vue' import type { App } from 'vue'
import type { Component } from '@vue/runtime-core' import type { Component } from '@vue/runtime-core'
import mockContext from 'unenv/runtime/mock/proxy' import mockContext from 'unenv/runtime/mock/proxy'
import type { NuxtApp } from './nuxt' import { NuxtApp, useRuntimeConfig } from './nuxt'
type Route = any type Route = any
type Store = any type Store = any
@ -92,19 +92,12 @@ function mock (warning: string) {
} }
const unsupported = new Set<keyof LegacyContext | keyof LegacyContext['ssrContext']>([ const unsupported = new Set<keyof LegacyContext | keyof LegacyContext['ssrContext']>([
'isClient',
'isServer',
'isStatic',
'store', 'store',
'target',
'spa', 'spa',
'env',
'modern',
'fetchCounters' 'fetchCounters'
]) ])
const todo = new Set<keyof LegacyContext | keyof LegacyContext['ssrContext']>([ const todo = new Set<keyof LegacyContext | keyof LegacyContext['ssrContext']>([
'isDev',
'isHMR', 'isHMR',
// Routing handlers - needs implementation or deprecation // Routing handlers - needs implementation or deprecation
'base', 'base',
@ -123,6 +116,15 @@ const todo = new Set<keyof LegacyContext | keyof LegacyContext['ssrContext']>([
const routerKeys: Array<keyof LegacyContext | keyof LegacyContext['ssrContext']> = ['route', 'params', 'query'] const routerKeys: Array<keyof LegacyContext | keyof LegacyContext['ssrContext']> = ['route', 'params', 'query']
const staticFlags = {
isClient: process.client,
isServer: process.server,
isDev: process.dev,
isStatic: undefined,
target: 'server',
modern: false
}
export const legacyPlugin = (nuxtApp: NuxtApp) => { export const legacyPlugin = (nuxtApp: NuxtApp) => {
nuxtApp._legacyContext = new Proxy(nuxtApp, { nuxtApp._legacyContext = new Proxy(nuxtApp, {
get (nuxt, p: keyof LegacyContext | keyof LegacyContext['ssrContext']) { get (nuxt, p: keyof LegacyContext | keyof LegacyContext['ssrContext']) {
@ -149,9 +151,12 @@ export const legacyPlugin = (nuxtApp: NuxtApp) => {
} }
} }
if (p === '$config') { if (p === '$config' || p === 'env') {
// TODO: needs implementation return useRuntimeConfig()
return mock('Accessing runtime config is not yet supported in Nuxt 3.') }
if (p in staticFlags) {
return staticFlags[p]
} }
if (p === 'ssrContext') { if (p === 'ssrContext') {