Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flat pageContext #1268

Open
brillout opened this issue Nov 20, 2023 · 0 comments
Open

Flat pageContext #1268

brillout opened this issue Nov 20, 2023 · 0 comments
Labels
enhancement ✨ New feature or request high-prio 💫

Comments

@brillout
Copy link
Member

brillout commented Nov 20, 2023

Plan

  • Flatten pageContext: merge all nested pageContext values directly onto pageContext => for a "think-free" pageContext that "just works".
  • Source information at pageContext.source: where the value comes from.
  • Precise access at pageContext.from: for conflict-free access.

Behind the scene, pageContext is a Proxy: upon property access the proxy goes through all sources (in a sensible order) and returns the first value it finds.

Examples

  • pageContext.user
    • pageContext.from.request.user (equivalent value, assuming there isn't any conflict)
    • pageContext.source.user: Source (see Source down below)
  • pageContext.Page: Component
    • pageContext.from.config[configFilePath].Page: Component
    • pageContext.from.configsOverridable.Page: SourceConfigsOverridable
    • pageContext.source.Page: Source
  • pageContext.passToClient: string
    • pageContext.from.configsCumulative.passToClient: SourceConfigsCumulative
    • pageContext.from.config[configFilePath].passToClient: string
    • pageContext.source.passToClient: Source
  • pageContext.isClientSideRenderable:
    • pageContext.from.configsComputed.isClientSideRenderable: SourceConfigsComputed
    • pageContext.source.isClientSideRenderable: Source
  • pageContext.data: Data
    • pageContext.from.hooks.data: SourceHooks
    • pageContext.from.hook[hookFilePath].data: Data
    • pageContext.source.data: Source
  • pageContext.locale: string
  • pageContext.pageId as well as:
    • pageContext.from.internal.pageId
    • pageContext.source.pageId: Source

Also:

  • pageContext.sources[0].Layout: Source
  • pageContext.sources[1].Layout: Source
  • ...

Use cases

  • Easier DX.
  • Adds all request properties to pageContext. (No need for the user to set pageContextInit anymore.)
  • Preserve pageContext of failed rendering, see Make original pageContext accessible to error page #1112.
  • Resolve conflicts. (That's why pageContext.from needs to hold value.)
  • Config validation.(E.g. Config document.title defined at ${pageContext.from.configsCumulative.document.values[i].definedAt} should be a string.)
  • Debug. (That's the only purpose of pageContext.from.config[configFilePath] and pageContext.from.hook[hookFilePath].)

Data Structures

// pageContext.source: Record<string, Source>

type Source = SourceConfigs | SourceHooks | SourceRenderFailure | SourceInternal | SourceRequest

type SourceInternal = {
  type: 'internal'
  value: unknown
}

type SourceRequest = {
  type: 'request'
  value: unknown
}

type SourceRenderFailure = {
  type: 'renderFailure'
  renderCount: number
  source: Source
}

type SourceHooks = {
  type: 'hooks'
  value: unknown, // e.g. `{ product: { name: 'iPhone', price: '499' } }`
  hookName: string // hook name e.g. 'data'
  definedAt: string
}

type SourceConfigs =
 | SourceConfigsOverridable
 | SourceConfigsCumulative
 | SourceConfigsComputed
/* Potential upcoming feature: resolve cumulative values at config-time instead of runtime,
   in order to save KBs on the client-side.
 | SourceConfigsResolved
 */

type SourceConfigsOverridable = {
  type: 'configsOverridable'
  value: unknown,
  definedAt: string
}
type SourceConfigsCumulative = {
  type: 'configsCumulative'
  values: {
      value: unknown,
      definedAt: string
  }[]
}
type SourceConfigsComputed = {
  type: 'configsComputed'
  value: unknown
}

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request high-prio 💫
Projects
None yet
Development

No branches or pull requests

1 participant