nitro icon indicating copy to clipboard operation
nitro copied to clipboard

allow syncing runtime config with full stack frameworks without depending on imports

Open pi0 opened this issue 2 years ago • 10 comments

Context: https://github.com/nuxt/nuxt/issues/14919

~~globalThis.__nitro__.useRuntimeConfig() could be useful for frameworks to use nitro runtime API without depending on bundler imports -- mainly for prerenderer preoptimizations --~~

(update: looking for a more secure way to allow access without imports but also avoid exposing nitro internals to globalThis)

pi0 avatar Jul 21 '23 10:07 pi0

/cc @rudolfbyker please tell if you like to work on this ❤️

pi0 avatar Jul 21 '23 10:07 pi0

I'd like to help, but I am very confused about where to start. I'll send you a string of dumb questions on discord, and then summarize the answers here when I feel like I have a better understanding of what needs to happen.

rudolfbyker avatar Jul 21 '23 11:07 rudolfbyker

We need to consider the security implications of exposing user provided variables in the global scope like this.

Hebilicious avatar Jul 22 '23 01:07 Hebilicious

We need to consider the security implications of exposing user provided variables in the global scope like this.

I don't think there is any security concern API lives in the same context (server runtime).

pi0 avatar Jul 24 '23 09:07 pi0

No response on Discord, so I'll copy it here:

What I mainly don't understand, is:

  • why we want to expose a function via globalThis rather than importing. Since nitro is a dependency of nuxt, it should always be available to import, right?
  • why this would help to solve the prerender/build entanglement (I'm willing to ignore this question and just take your word for it, if it's hard to explain)

rudolfbyker avatar Jul 25 '23 06:07 rudolfbyker

@pi0 I would love to move forward with this, but I need some guidance, please. See my questions above.

rudolfbyker avatar Aug 14 '23 06:08 rudolfbyker

Hi dear @rudolfbyker sorry was little busy in past weeks and forgot to followup on this.

why we want to expose a function via globalThis rather than importing. Since nitro is a dependency of nuxt, it should always be available to import, right?

Nitro it is a dependency of nuxt that's correct. Also we currently directly import useRuntimeConfig in nuxt app. But that's the problem! Because of this import, we have a direct (bundling) dependency between nuxt and nitro and that requires the prerenderer to need bundling nuxt app as well in order to have direct access.

pi0 avatar Aug 14 '23 07:08 pi0

No problem @pi0 :)

I think I'm starting to understand. So this is a kind of workaround to get around the bundler's default behavior?

Adding something to globalThis is a side effect, obviously. But I'm not 100% sure when this side effect should happen. Is there an example of something else in this repo that already follows the pattern of adding things to globalThis, so that I can see when it happens?

Because as far as I understand, if you just have globalThis.foo = bar somewhere in a file, it will only run once that file is imported, anyway. So when does this import happen? :thinking:

rudolfbyker avatar Aug 14 '23 10:08 rudolfbyker

It can leave anywhere in entry (config.ts should also do this) because we lazy load nuxt routes they surely happen after that code being executed.

pi0 avatar Aug 14 '23 14:08 pi0

I struggle to understand your last message. Sorry!

It can leave anywhere in entry

  1. Do you mean "live"? Sorry, not trying to nit-pick. Just confused.
  2. What do you mean by "entry"?
    • One or all of these files? https://github.com/unjs/nitro/tree/main/src/runtime/entries
    • The index.ts (or dist/index.mjs) file?

config.ts should also do this

Do you mean this file? https://github.com/unjs/nitro/blob/main/src/config.ts


I see defineNitroConfig made available as globalThis.defineNitroConfig at https://github.com/unjs/nitro/blob/main/src/options.ts#L144 . Should that also move to globalThis.__nitro__.defineNitroConfig at some point? :thinking: If so, maybe there should be a centralized place where __nitro__ is populated, so that we can write types for __nitro__ more easily.

For now I guess we can simply set globalThis.__nitro__.useRuntimeConfig right after the function definition? Or did you mean to say that the globals need to be set in each one of these files? https://github.com/unjs/nitro/tree/main/src/runtime/entries

rudolfbyker avatar Aug 18 '23 15:08 rudolfbyker