You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Add a dedicated compatibility section detailing supported Python and Django versions.
- Simplify the `INSTALLED_APPS` setup instruction.
- Clarify the behavior of the `stream` parameter and its future default value.
- Provide a warning about HTML customization limitations with the Next.js App Router.
- Enhance overall readability, accuracy of setup instructions, and usage examples.
- Correct minor grammatical errors and typos.
@@ -19,34 +24,30 @@ that need both Django pages (usually rendered by Django templates) and Next.js p
19
24
If this sounds like you, **this package is the perfect fit**. ✅
20
25
21
26
However, if you’re starting a new project and intend to use Django purely as an API backend with Next.js as a standalone frontend, you **don’t need** this package.
22
-
Simply run both servers and configure your public web server to point to Next.js for a straightforward setup.
27
+
Simply run both servers and configure your public web server to route requests to Next.js; this provides a more straightforward setup.
23
28
24
29
## How does it work?
25
30
26
-
When a user opens a page, django receives the initial request, queries the Next.js server for the HTML response, and returns it to the user.
31
+
When a user opens a page, Django receives the initial request, queries the Next.js server for the HTML response, and returns it to the user.
27
32
After opening a Next.js page, the user can navigate to other Next.js pages without any additional requests to Django (the Next.js server handles the routing).
28
33
29
-
This is how it looks like in production:
34
+
This is what it looks like in production:
30
35
31
36

32
37
33
-
In development, to simplify the setup and remove the need to a reverse proxy like Nginx, Django also acts as the reverse proxy for Next.js client-side requests.
34
-
35
-
## Getting Started
38
+
In development, to simplify the setup and remove the need for a reverse proxy like Nginx, Django also acts as the reverse proxy for Next.js client-side requests.
36
39
37
-
- Install the latest version from PyPI.
40
+
## Getting started
38
41
39
-
```shell
40
-
pip install django-nextjs
41
-
```
42
+
Install the latest version from PyPI:
42
43
43
-
- Add `django_nextjs.apps.DjangoNextJSConfig` to `INSTALLED_APPS`.
44
-
45
-
- Set up Next.js URLs depending on your environment.
44
+
```shell
45
+
pip install django-nextjs
46
+
```
46
47
47
-
## Setup Next.js URLs (Development Environment)
48
+
Add `django_nextjs` to `INSTALLED_APPS`.
48
49
49
-
Configure your `asgi.py` with `NextJsMiddleware` as shown below:
50
+
Configure your project's `asgi.py` with `NextJsMiddleware` as shown below:
50
51
51
52
```python
52
53
import os
@@ -91,7 +92,7 @@ urlpatterns = [
91
92
> [!IMPORTANT]
92
93
> Using ASGI is **required**
93
94
> for [fast refresh](https://nextjs.org/docs/architecture/fast-refresh)
94
-
> to work properly in Next.js 12+.
95
+
> to work properly in Next.js 12 and later.
95
96
> Without it, you'll need to manually refresh your browser
96
97
> to see changes during development.
97
98
>
@@ -100,17 +101,17 @@ urlpatterns = [
100
101
> or [Uvicorn](https://www.uvicorn.org/).
101
102
102
103
103
-
## Setup Next.js URLs (Production Environment)
104
+
## Setup Next.js URLs in production
104
105
105
-
In production, use a reverse proxy like Nginx or Caddy:
106
+
In production, use a reverse proxy like Nginx or Caddy.
Even though it's not recommended, sometimes you might need to add some custom steps before showing a Next.js page in Django. However, **we advise moving this logic to Next.js to ensure it's applied even during client-side navigation**. If you find yourself in this situation, you can create an asynchronous view for each page as demonstrated below:
159
-
160
-
```python
161
-
from django_nextjs.render import render_nextjs_page
162
-
163
-
asyncdefjobs(request):
164
-
# Your custom logic
165
-
returnawait render_nextjs_page(request)
166
-
```
167
-
168
-
#### Using `nextjs_page` with `stream=True` (Recommended)
169
-
170
-
If you're using the [Next.js App Router](https://nextjs.org/docs/app) (introduced in Next.js 13+), you can enable streaming by setting the `stream=True` parameter in the `nextjs_page` function. This allows the HTML response to be streamed directly from the Next.js server to the client. This approach is particularly useful for server-side rendering with streaming support to show an [instant loading state](https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming#instant-loading-states) from the Next.js server while the content of a route segment loads.
If you're using the [Next.js App Router](https://nextjs.org/docs/app) (introduced in Next.js 13), you can enable streaming by setting the `stream` parameter to `True` in the `nextjs_page` function. This allows the HTML response to be streamed directly from the Next.js server to the client. This approach is particularly useful for server-side rendering with streaming support to display an [instant loading state](https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming#instant-loading-states) from the Next.js server while the content of a route segment loads.
183
156
184
-
- When using `stream_nextjs_page`, you cannot use a custom HTML template in Django, as the HTML is streamed directly from the Next.js server.
185
-
- The `stream` parameter will default to `True` in future releases. Currently, it is set to `False` for backward compatibility. To avoid breaking changes, we recommend explicitly setting `stream=False` if you are customizing HTML and do not want to use streaming.
157
+
Currently, the default value for this parameter
158
+
is set to `False` for backward compatibility.
159
+
It will default to `True` in the next major release.
186
160
187
-
## Customizing the HTML Response
161
+
## Customizing the HTML response
188
162
189
163
You can modify the HTML code that Next.js returns in your Django code.
190
164
191
-
Avoiding duplicate code for the navbar and footer is a common use case
192
-
for this if you are using both Next.js and Django templates.
165
+
> [!WARNING]
166
+
> This feature is not compatible with the Next.js App Router, and to use it,
167
+
> you need to set the `stream` parameter to `False` in the `nextjs_page` function.
168
+
> Because of these limitations, we do not recommend using this feature.
169
+
> For more details, please refer to [this GitHub issue](https://github.com/QueraTeam/django-nextjs/issues/22).
170
+
171
+
This is a common use case for avoiding duplicate code for the navbar and footer if you are using both Next.js and Django templates.
193
172
Without it, you would have to write and maintain two separate versions
194
173
of your navbar and footer (a Django template version and a Next.js version).
195
174
However, you can simply create a Django template for your navbar and insert its code
196
-
at the beginning of `<body>` tag returned from Next.js.
175
+
at the beginning of the `<body>` tag in the HTML returned by Next.js.
197
176
198
-
To enable this feature, you need to customize the document and root layout
199
-
in Next.js and make the following adjustments:
177
+
To enable this feature, you need to customize the Next.js `pages/_document` file and make the following adjustments:
200
178
201
-
- Add `id="__django_nextjs_body"` as the first attribute of `<body>` element.
179
+
- Add `id="__django_nextjs_body"` as the first attribute of the `<body>` element.
202
180
- Add `<div id="__django_nextjs_body_begin" />` as the first element inside `<body>`.
203
181
- Add `<div id="__django_nextjs_body_end" />` as the last element inside `<body>`.
204
182
205
-
NOTE: Currently HTML customization is not working with [app router](https://nextjs.org/docs/app) (Next.js 13+).
- To avoid "Too many redirects" error, you may need to add `APPEND_SLASH = False` in your Django project's `settings.py`. Also, do not add `/` at the end of nextjs paths in `urls.py`.
237
+
- To avoid "Too many redirects" errors, you may need to add `APPEND_SLASH = False` in your Django project's `settings.py`. Also, do not add `/` at the end of Next.js paths in `urls.py`.
283
238
- This package does not provide a solution for passing data from Django to Next.js. The Django Rest Framework, GraphQL, or similar solutions should still be used.
284
-
-The Next.js server will not be run by this package. You will need to run it yourself.
239
+
-This package does not start the Next.js server. You need to run it yourself.
285
240
286
241
## Settings
287
242
@@ -297,25 +252,26 @@ NEXTJS_SETTINGS = {
297
252
298
253
### `nextjs_server_url`
299
254
300
-
The URL of Next.js server (started by `npm run dev` or `npm run start`)
255
+
The URL of the Next.js server (started by `npm run dev` or `npm run start`)
301
256
302
257
### `ensure_csrf_token`
303
258
304
259
If the user does not have a CSRF token, ensure that one is generated and included in the initial request to the Next.js server by calling Django's `django.middleware.csrf.get_token`. If `django.middleware.csrf.CsrfViewMiddleware` is installed, the initial response will include a `Set-Cookie` header to persist the CSRF token value on the client. This behavior is enabled by default.
305
260
306
-
**When you need `ensure_csrf_token`?**
307
-
308
-
You may need to issue GraphQL POST requests to fetch data in Next.js `getServerSideProps`. If this is the user's first request, there will be no CSRF cookie, causing the request to fail since GraphQL uses POST even for data fetching.
309
-
In this case this option solves the issue,
310
-
and as long as `getServerSideProps` functions are side-effect free (i.e., they don't use HTTP unsafe methods or GraphQL mutations), it should be fine from a security perspective. Read more [here](https://docs.djangoproject.com/en/3.2/ref/csrf/#is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability).
261
+
> [!TIP]
262
+
> **The use case for this option**
263
+
>
264
+
> You may need to issue GraphQL POST requests to fetch data in Next.js `getServerSideProps`. If this is the user's first request, there will be no CSRF cookie, causing the request to fail since GraphQL uses POST even for data fetching.
265
+
> In this case, this option solves the issue,
266
+
> and as long as `getServerSideProps` functions are side-effect free (i.e., they don't use HTTP unsafe methods or GraphQL mutations), it should be fine from a security perspective. Read more [here](https://docs.djangoproject.com/en/3.2/ref/csrf/#is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability).
311
267
312
268
### `dev_proxy_paths`
313
269
314
270
A list of paths that should be proxied to the Next.js server in development mode.
315
271
316
-
This is useful if you want to use a custom path instead of `/next` inside the `public` directory of Next.js.
272
+
This option is useful if you want to use a custom path instead of `/next` inside the Next.js `public` directory.
317
273
For example, if you want to use `/static-next` instead of `/next`, you can set `proxy_paths` to `["/_next", "/__next", "/static-next"]`
318
-
and place your static files in `public/static-next` directory of Next.js.
274
+
and place your static files in the `public/static-next` directory.
319
275
You should also update the production reverse proxy configuration accordingly.
320
276
321
277
## Contributing
@@ -325,7 +281,7 @@ To start development:
325
281
- Install development dependencies in your virtualenv with `pip install -e '.[dev]'`
326
282
- Install pre-commit hooks using `pre-commit install`.
327
283
328
-
Love django-next.js? 🌟 Star us on GitHub to help the project grow!
284
+
Love django-nextjs? 🌟 Star us on GitHub to help the project grow!
0 commit comments