Skip to content

Commit

Permalink
Merge branch 'canary' into use-bun
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Aug 2, 2023
2 parents 444c558 + 57788b5 commit e243278
Show file tree
Hide file tree
Showing 33 changed files with 260 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,22 @@ async function create(data) {
}
```
### Redirection
You can also trigger a redirection within a Server Action by using the `redirect` function.
```js highlight={8}
import { redirect } from 'next/navigation'

async function addItem(data) {
'use server'

await saveToDb({ data })

redirect('/success')
}
```
## Glossary
### Actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ related:

As an alternative to having the special Next.js `app` or `pages` directories in the root of your project, Next.js also supports the common pattern of placing application code under the `src` directory.

This separates application code from project configuration files which mostly live in the root of a project. Which is preferred by some individuals and teams.
This separates application code from project configuration files which mostly live in the root of a project, which is preferred by some individuals and teams.

To use the `src` directory, move the `app` Router folder or `pages` Router folder to `src/app` or `src/pages` respectively.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ With this configuration, your application **will produce an error** if you try t

The following additional dynamic features are not supported with a static export:

- [Dynamic Routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) without `generateStaticParams()`
- `rewrites` in `next.config.js`
- `redirects` in `next.config.js`
- `headers` in `next.config.js`
Expand Down
60 changes: 52 additions & 8 deletions docs/02-app/02-api-reference/04-functions/cookies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,70 @@ async function create(data) {

## Deleting cookies

To "delete" a cookie, you must set a new cookie with the same name and an empty value. You can also set the `maxAge` to `0` to expire the cookie immediately.
> **Good to know**: You can only delete cookies in a [Server Action](/docs/app/building-your-application/data-fetching/server-actions) or [Route Handler](/docs/app/building-your-application/routing/route-handlers).
There are several options for deleting a cookie:

### `cookies().delete(name)`

You can explicitly delete a cookie with a given name.

```js filename="app/actions.js"
'use server'

import { cookies } from 'next/headers'

async function create(data) {
cookies().delete('name')
}
```

### `cookies().set(name, '')`

Alternatively, you can set a new cookie with the same name and an empty value.

```js filename="app/actions.js"
'use server'

import { cookies } from 'next/headers'

async function create(data) {
cookies().set('name', '')
}
```

> **Good to know**: `.set()` is only available in a [Server Action](/docs/app/building-your-application/data-fetching/server-actions) or [Route Handler](/docs/app/building-your-application/routing/route-handlers).
### `cookies().set(name, value, { maxAge: 0 })`

Setting `maxAge` to 0 will immediately expire a cookie.

```js filename="app/actions.js"
'use server'

import { cookies } from 'next/headers'

async function create(data) {
cookies().set({
name: 'name',
value: '',
expires: new Date('2016-10-05'),
path: '/', // For all paths
})
cookies().set('name', 'value', { maxAge: 0 })
}
```

### `cookies().set(name, value, { expires: timestamp })`

Setting `expires` to any value in the past will immediately expire a cookie.

```js filename="app/actions.js"
'use server'

import { cookies } from 'next/headers'

async function create(data) {
const oneDay = 24 * 60 * 60 * 1000
cookies().set('name', 'value', { expires: Date.now() - oneDay })
}
```

You can only set cookies that belong to the same domain from which `.set()` is called. Additionally, the code must be executed on the same protocol (HTTP or HTTPS) as the cookie you want to update.
> **Good to know**: You can only delete cookies that belong to the same domain from which `.set()` is called. Additionally, the code must be executed on the same protocol (HTTP or HTTPS) as the cookie you want to delete.
## Version History

Expand Down
2 changes: 1 addition & 1 deletion examples/cms-enterspeed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example showcases Next.js's [Static Generation](https://nextjs.org/docs/bas

## Demo

### [https://next-blog-wordpress.vercel.app](https://next-blog-wordpress.vercel.app)
### [https://next-blog-demo.enterspeed.com/](https://next-blog-demo.enterspeed.com/)

## Deploy your own

Expand Down
83 changes: 0 additions & 83 deletions examples/with-supabase/app/_examples/protected-route/page.tsx

This file was deleted.

33 changes: 33 additions & 0 deletions examples/with-supabase/app/auth/sign-in/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const formData = await request.formData()
const email = String(formData.get('email'))
const password = String(formData.get('password'))
const supabase = createRouteHandlerClient({ cookies })

const { error } = await supabase.auth.signInWithPassword({
email,
password,
})

if (error) {
return NextResponse.redirect(
`${requestUrl.origin}/login?error=Could not authenticate user`,
{
// a 301 status is required to redirect from a POST to a GET route
status: 301,
}
)
}

return NextResponse.redirect(requestUrl.origin, {
// a 301 status is required to redirect from a POST to a GET route
status: 301,
})
}
17 changes: 17 additions & 0 deletions examples/with-supabase/app/auth/sign-out/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const supabase = createRouteHandlerClient({ cookies })

await supabase.auth.signOut()

return NextResponse.redirect(`${requestUrl.origin}/login`, {
// a 301 status is required to redirect from a POST to a GET route
status: 301,
})
}
39 changes: 39 additions & 0 deletions examples/with-supabase/app/auth/sign-up/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

export async function POST(request: Request) {
const requestUrl = new URL(request.url)
const formData = await request.formData()
const email = String(formData.get('email'))
const password = String(formData.get('password'))
const supabase = createRouteHandlerClient({ cookies })

const { error } = await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${requestUrl.origin}/auth/callback`,
},
})

if (error) {
return NextResponse.redirect(
`${requestUrl.origin}/login?error=Could not authenticate user`,
{
// a 301 status is required to redirect from a POST to a GET route
status: 301,
}
)
}

return NextResponse.redirect(
`${requestUrl.origin}/login?message=Check email to continue sign in process`,
{
// a 301 status is required to redirect from a POST to a GET route
status: 301,
}
)
}
23 changes: 23 additions & 0 deletions examples/with-supabase/app/login/messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client'

import { useSearchParams } from 'next/navigation'

export default function Messages() {
const searchParams = useSearchParams()
const error = searchParams.get('error')
const message = searchParams.get('message')
return (
<>
{error && (
<p className="mt-4 p-4 bg-neutral-900 text-neutral-300 text-center">
{error}
</p>
)}
{message && (
<p className="mt-4 p-4 bg-neutral-900 text-neutral-300 text-center">
{message}
</p>
)}
</>
)
}
Loading

0 comments on commit e243278

Please sign in to comment.