Closed jamesarosen closed 1 month ago
It's unclear to me if it's best way to solve this, but this can be addressed with following:
have import process from 'node:process'
and somewhere in global scope of written file assign that process
to global
space - i.e. globalThis.process = process
and finally adjust bundling config ( https://github.com/withastro/adapters/blob/f14f4d72befcb715da806fea54a8245e8f052556/packages/netlify/src/index.ts#L259-L269 ) to allow importing node:process
as with current configuration it would fail (likely due to neutral
platform
being used). In my quick test I just added node:process
as external
and this resulted in successful deploy with being able to use both import.meta.env
and process.env
in my test middleware.
Wether this is best way to actually fix it or not is a bit TBD, but seems like what is needed is to make process
available on global.
Another way (might be more idiomatic way to provide global than above) is to use inject
feature of esbuild
( https://esbuild.github.io/api/#inject )
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When using Netlify's Edge Runtime, Astro compiles
import.meta.env.FOO
toprocess.env.FOO
, butprocess.env
doesn't exist for that runtime.How I found this
astro build
adds the Astro component runtime to the site's middleware stack if both (a) the middleware and (b) a.astro
Page or Componentimport
the same shared library fromsrc/lib/
, even if that library has nothing to do with Astro or HTML.In b09bfe5a, the
middleware.ts
shares nothing with the*.astro
files.process.env
does not appear in the compiledmiddleware.mjs
. The compiledmiddleware.mjs
is 56710 bytes. It includes thecookie
andastro/dist/core
modules.In 4551a260, the
middleware.ts
andCard.astro
share an import, though the import is pure JS and unrelated to Astro. This import causes the compiledmiddleware.mjs
to gain 66721 bytes, 1600 new lines, and thecssesc
,kleur
,html-escaper
, andclsx
packages. Most notably, it adds aprocess.env
reference that breaks the Netlify build.In 4fba3cf5, I show that just adding an
import.meta.env.NODE_ENV
reference to the middleware is enough to break the Netlify build.I get this in my site's logs, though the line numbers don't match up with what's compiled here:
What's the expected result?
The the middleware doesn't reference any Astro components or pages, the compiled middleware code should not be affected by
*.astro
files importing the same files that it does. The runtimes should be isolated.According to the Netlify Edge Functions docs, Astro should probably compile those references to
Netlify.env.get
.Link to Minimal Reproducible Example
https://github.com/jamesarosen/astro-broken-middleware-demo
Participation