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

Create a .dev.vars file for new projects #189

Open
james-elicx opened this issue Dec 18, 2024 · 7 comments
Open

Create a .dev.vars file for new projects #189

james-elicx opened this issue Dec 18, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@james-elicx
Copy link
Collaborator

We should create a .dev.vars file as part of C3 when someone creates a new Next.js project, or when they migrate over to using OpenNext (related: #188).

This file should include a NEXTJS_ENV variable out-of-the-box which defaults to development. This will be used for determining whether the app is running in development or production locally, so that inlined .env files can be used in the worker.

@james-elicx james-elicx added the enhancement New feature or request label Dec 18, 2024
@himanshu-ntml
Copy link

Screenshot 2024-12-21 at 2 26 20 PM Screenshot 2024-12-21 at 2 26 30 PM Screenshot 2024-12-21 at 2 26 38 PM

I just created a app using getting started but .env.development file not loading.

@vicb
Copy link
Contributor

vicb commented Dec 21, 2024

NODE_ENV is forced to "production" but you should have access to TEST_ENV_VAR?
If not please try to attach a repro in a GH repo

@james-elicx
Copy link
Collaborator Author

I just created a app using getting started but .env.development file not loading.

@himanshu-ntml can you confirm whether the route is being SSR'd or SSG'd? I have just created a dummy project and forced the API route to be SSR'd and it was returning the variable. By default, Next.js tries to SSG everything unless it's unable to.

image image

@himanshu-ntml
Copy link

himanshu-ntml commented Dec 21, 2024

@james-elicx i tried with api route too, I got same behaviour. I was still seeing node_env error too. Thank you so much for swift response.

@himanshu-ntml
Copy link

I guess i missed "force-dynamic". I am not sure why it works with "force dynamic"

I did get some warnings

himanshu@Himanshus-MacBook-Pro v3-open-next % npm run preview

> [email protected] preview
> opennextjs-cloudflare && wrangler dev


┌─────────────────────────────┐
│ OpenNext — Cloudflare build │
└─────────────────────────────┘

App directory: /Users/himanshu/Desktop/experiments/v3-open-next
Next.js version : 14.2.5
OpenNext v3.3.0

┌─────────────────────────────────┐
│ OpenNext — Building Next.js app │
└─────────────────────────────────┘


> [email protected] build
> next build

  ▲ Next.js 14.2.5

   Creating an optimized production build ...

warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
warn - https://tailwindcss.com/docs/content-configuration
 ✓ Compiled successfully
 ✓ Linting and checking validity of types    
 ✓ Collecting page data    
 ✓ Generating static pages (5/5)
 ✓ Collecting build traces    
 ✓ Finalizing page optimization    

Route (app)                              Size     First Load JS
┌ ○ /                                    137 B          87.2 kB
├ ○ /_not-found                          871 B          87.9 kB
└ ƒ /api                                 0 B                0 B
+ First Load JS shared by all            87 kB
  ├ chunks/23-b75664ace61c0abb.js        31.5 kB
  ├ chunks/fd9d1056-2821b0f0cabcd8bd.js  53.6 kB
  └ other shared chunks (total)          1.86 kB


○  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand


┌──────────────────────────────┐
│ OpenNext — Generating bundle │
└──────────────────────────────┘

Bundling middleware function...
Bundling static assets...
Bundling cache assets...
Building server function: default...
# copyPackageTemplateFiles
⚙️ Bundling the OpenNext server...

# patchWranglerDeps
# updateWebpackChunksFile
 - chunk 347.js
 - chunk 682.js
 - chunk 948.js
▲ [WARNING] Suspicious assignment to defined constant "process.env.NODE_ENV" [assign-to-define]

    .open-next/server-functions/default/index.mjs:551:2:
      551 │   process.env.NODE_ENV = process.env.NODE_ENV ?? "production";
          ╵   ~~~~~~~~~~~~~~~~~~~~

  The expression "process.env.NODE_ENV" has been configured to be replaced with a constant using the
  "define" feature. If this expression is supposed to be a compile-time constant, then it doesn't
  make sense to assign to it here. Or if this expression is supposed to change at run-time, this
  "define" substitution should be removed.

Applying code patches:
 - patching require
 - patching `buildId` function
 - patching `loadManifest` function
 - patching next's require
 - patching `findDir` function
 - patching `evalManifest` function
 - patching cacheHandler
 - patching 'require(this.middlewareManifestPath)'
 - patching exception bubbling
 - patching `loadInstrumentationModule` function
 - patching `patchAsyncStorage` call
 - patching `eval("require")` calls
 - patching `require.resolve` call
All 13 patches applied

Worker saved in `/Users/himanshu/Desktop/experiments/v3-open-next/.open-next/server-functions/default/handler.mjs` 🚀

OpenNext build complete.

 ⛅️ wrangler 3.99.0
-------------------

Using vars defined in .dev.vars
Your worker has access to the following bindings:
- Vars:
  - NEXTJS_ENV: "(hidden)"
  - NODE_ENV: "(hidden)"
▲ [WARNING] Suspicious assignment to defined constant "process.env.NODE_ENV" [assign-to-define]

    .open-next/server-functions/default/handler.mjs:32303:4:
      32303 │     process.env.NODE_ENV = "production";
            ╵     ~~~~~~~~~~~~~~~~~~~~

  The expression "process.env.NODE_ENV" has been configured to be replaced with a constant using the "define" feature. If this expression is supposed to be a compile-time constant, then it doesn't make sense to assign to it here. Or if this expression is supposed to change at run-time, this "define" substitution should be removed.


⎔ Starting local server...
[wrangler:inf] Ready on http://localhost:8787
[wrangler:inf] GET /_next/static/chunks/23-b75664ace61c0abb.js 200 OK (81ms)
[wrangler:inf] GET /_next/static/media/a34f9d1faa5f3315-s.p.woff2 200 OK (86ms)
[wrangler:inf] GET /_next/static/css/cf3d98e76e393f82.css 200 OK (83ms)
[wrangler:inf] GET /_next/static/chunks/webpack-d0ceac4fb78a3613.js 200 OK (83ms)
[wrangler:inf] GET /_next/static/chunks/main-app-db3b0bcd7d12209c.js 200 OK (82ms)
[wrangler:inf] GET / 200 OK (115ms)
[wrangler:inf] GET /_next/static/chunks/fd9d1056-2821b0f0cabcd8bd.js 200 OK (28ms)
▲ [WARNING] workerd/io/worker.c++:1195: warning: NOSENTRY Warning: Cross Request Promise Resolve

  1035013bf 103ad2d2b 103ad37cf 103bddc9f 1035aaab3 103621f47 1036216eb 10353e4ef 103512e13
  10382aa57 10382b5e3 10382b76b 1038420e7 103841ebb 1029d9377 1029d8f4f 1029d333f 1029cf5ff
  1029c95b7 1024aa37f 1024ab643 1024ab3e7 1048b60e3 1048baffb 1048bcfbb 1048b51fb 1024214eb
  104907983 104907c93


[wrangler:inf] GET / 304 Not Modified (14ms)
[wrangler:inf] GET / 200 OK (15ms)
[wrangler:inf] GET /_next/static/media/a34f9d1faa5f3315-s.p.woff2 304 Not Modified (3ms)
[wrangler:inf] GET /_next/static/css/cf3d98e76e393f82.css 304 Not Modified (1ms)
[wrangler:inf] GET /_next/static/chunks/fd9d1056-2821b0f0cabcd8bd.js 304 Not Modified (3ms)
[wrangler:inf] GET /_next/static/chunks/main-app-db3b0bcd7d12209c.js 304 Not Modified (3ms)
[wrangler:inf] GET /_next/static/chunks/23-b75664ace61c0abb.js 304 Not Modified (2ms)
[wrangler:inf] GET /api 200 OK (16ms)
⎔ Shutting down local server...

@james-elicx
Copy link
Collaborator Author

I guess i missed "force-dynamic". I am not sure why it works with "force dynamic"

It's because it's forcing the route to be SSR'd. A blank API route that returns a fixed response will try to be SSG'd by Next.js.

@himanshu-ntml
Copy link

Thank you, that makes sense.

I’m curious to know why I’m still getting NODE_ENV warnings. Any ideas?

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

No branches or pull requests

3 participants