-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve error message upon using Telefunc for SSR
- Loading branch information
Showing
5 changed files
with
60 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
import { AsyncLocalStorage } from 'async_hooks' | ||
import { assert, isObject, getGlobalObject, assertUsage } from '../../utils' | ||
import { assert, assertWarning, isObject, getGlobalObject, assertUsage } from '../../utils' | ||
import { installAsyncMode } from '../getContext' | ||
import { provideErrMsg } from './provideErrMessage' | ||
import type { Telefunc } from './TelefuncNamespace' | ||
|
||
export { getContext_async } | ||
export { provideTelefuncContext_async } | ||
|
||
const globalObject = getGlobalObject<{ asyncStore?: AsyncLocalStorage<Telefunc.Context> }>('getContext/async.ts', {}) | ||
|
||
installAsyncMode({ getContext_async, provideTelefuncContext_async, restoreContext_async, getContextOptional_async }) | ||
installAsyncMode({ getContext_async, provideTelefuncContext_async, restoreContext_async }) | ||
|
||
function getContext_async(): Telefunc.Context { | ||
assert(globalObject.asyncStore) | ||
const errMsg = '[getContext()] Make sure to call provideTelefuncContext() before calling getContext()' | ||
assertUsage(globalObject.asyncStore, errMsg) | ||
const context = globalObject.asyncStore.getStore() | ||
assert(context === undefined || isObject(context)) | ||
assertUsage(context, provideErrMsg) | ||
assertUsage(context, errMsg) | ||
return context | ||
} | ||
|
||
function provideTelefuncContext_async(context: Telefunc.Context): void { | ||
assert(isObject(context)) | ||
assertUsage(isObject(context), '[provideTelefuncContext(context)] Argument `context` should be an object') | ||
globalObject.asyncStore = globalObject.asyncStore || new AsyncLocalStorage() | ||
globalObject.asyncStore.enterWith(context) | ||
} | ||
|
||
function getContextOptional_async(): any { | ||
assert(false) | ||
} | ||
|
||
function restoreContext_async(..._args: any[]): any { | ||
assert(false) | ||
function restoreContext_async(context: null | Telefunc.Context): any { | ||
assert(context === null || isObject(context)) | ||
assertWarning( | ||
!context, | ||
'When using `provideTelefuncContext()` (i.e. Async Hooks), then providing the `context` object to the server middleware `telefunc()` has no efect.', | ||
{ onlyOnce: true } | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,58 @@ | ||
import { assert, isObject, getGlobalObject, assertUsage } from '../../utils' | ||
import { provideErrMsg } from './provideErrMessage' | ||
import type { Telefunc } from './TelefuncNamespace' | ||
|
||
export { getContext_sync } | ||
export { provideTelefuncContext_sync } | ||
export { restoreContext_sync } | ||
export { getContextOptional_sync } | ||
|
||
// Using the global scope is needed for Next.js. I'm guessing that Next.js is including the `node_modules/` files in a seperate bundle than user files. | ||
const globalObject = getGlobalObject<{ context: null | Telefunc.Context; isRestored: boolean }>('getContext/sync.ts', { | ||
const globalObject = getGlobalObject<{ | ||
context: null | Telefunc.Context | ||
hasRestoreAccess: boolean | ||
neverProvided: boolean | ||
neverRestored: boolean | ||
}>('getContext/sync.ts', { | ||
context: null, | ||
isRestored: false | ||
hasRestoreAccess: false, | ||
neverProvided: true, | ||
neverRestored: true | ||
}) | ||
|
||
function getContext_sync(): Telefunc.Context { | ||
const { context, isRestored } = globalObject | ||
assert(context === null || isObject(context)) | ||
const errMsg = isRestored | ||
? provideErrMsg | ||
: '[getContext()] Cannot access context object, see https://telefunc.com/getContext#access' | ||
assertUsage(context !== null, errMsg) | ||
if (globalObject.context === null) { | ||
// Using `neverRestored` to detect SSR doesn't always work. | ||
if (globalObject.neverRestored) { | ||
assertUsage(false, 'Using Telefunc to fetch the initial data of your page is discouraged, see https://telefunc.com/initial-page-data') // prettier-ignore | ||
} | ||
if (globalObject.hasRestoreAccess || globalObject.neverProvided) { | ||
assertUsage(false, '[getContext()] Make sure to provide a context object, see https://telefunc.com/getContext#provide') // prettier-ignore | ||
} else { | ||
assertUsage(false, '[getContext()] Cannot access context object, see https://telefunc.com/getContext#access') | ||
} | ||
} | ||
const { context } = globalObject | ||
assert(isObject(context)) | ||
return context | ||
} | ||
|
||
function getContextOptional_sync() { | ||
return globalObject.context | ||
} | ||
|
||
function restoreContext_sync(context: null | Telefunc.Context) { | ||
provide(context, true) | ||
globalObject.neverRestored = false | ||
provide(context) | ||
} | ||
|
||
function provideTelefuncContext_sync(context: Telefunc.Context) { | ||
provide(context, false) | ||
assertUsage(isObject(context), '[provideTelefuncContext(context)] Argument `context` should be an object') | ||
provide(context) | ||
} | ||
|
||
function provide(context: null | Telefunc.Context, isRestored: boolean) { | ||
function provide(context: null | Telefunc.Context) { | ||
assert(context === null || isObject(context)) | ||
if (context) { | ||
globalObject.neverProvided = false | ||
globalObject.context = context | ||
} | ||
globalObject.isRestored = isRestored | ||
globalObject.hasRestoreAccess = true | ||
process.nextTick(() => { | ||
globalObject.context = null | ||
globalObject.isRestored = false | ||
globalObject.hasRestoreAccess = false | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters