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

Minimum set of changes to make bindings work locally #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# wrangler
.wrangler
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template we give people should ignore .wrangler, otherwise you end up committing the temp state of local dev to git history accidentally.

14 changes: 14 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@
const nextConfig = {}

module.exports = nextConfig

// Everything below *needs* to be part of the default template that running npm create cloudflare@latest gives you
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ needs to be default, not opt-in.

// It needs to be documented in real dev docs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be loud, at the top of any getting started path we have (since many people will have existing Next.js apps, or be creating them outside of npm create cloudflare@latest, outside of dash.

// It needs to be the happy path, not optional thing you discover later
// Assume that every single developer will use one or more of these in their app
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ really important.

if (process.env.NODE_ENV === 'development') {
import('@cloudflare/next-on-pages/__experimental__next-dev').then(({ setupDevBindings }) => {

// When Pages has wrangler.toml, just read from that in current directory and can skip this config
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big part of why getting Pages wrangler.toml matters so much.

setupDevBindings({
kvNamespaces: ['MY_KV_1', 'MY_KV_2'],
});
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ this is effectively making people re-create what would otherwise be in wrangler.toml.

});
}
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
"react-dom": "^18"
},
"devDependencies": {
"@cloudflare/next-on-pages": "^1.8.2",
"@cloudflare/next-on-pages": "^0.0.0-2b5c8f2",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"miniflare": "^3.20231030.4",
"typescript": "^5",
"vercel": "^33.0.1"
}
}
}
6 changes: 5 additions & 1 deletion src/app/api/hello/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ import type { NextRequest } from 'next/server'
export const runtime = 'edge'

export async function GET(request: NextRequest) {
return new Response(JSON.stringify({ name: 'John Doe' }))
const myKv = process.env['MY_KV_1'];
await myKv.put('foo', 'bar');
const valueFromKv = await myKv.get('foo');

return new Response(JSON.stringify({ name: valueFromKv }))
}