Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
ab3f707e
Unverified
Commit
ab3f707e
authored
Jun 15, 2023
by
Wyatt Johnson
Committed by
GitHub
Jun 15, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Route Module Updates Redux (#51373)
Small QOL improvements to `RenderResult` and `RouteModule` setup.
parent
ce4aa787
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
92 additions
and
90 deletions
+92
-90
base-server.ts
packages/next/src/server/base-server.ts
+29
-18
route-handler-manager.ts
...er/future/route-handler-managers/route-handler-manager.ts
+0
-5
module.ts
.../next/src/server/future/route-modules/app-route/module.ts
+0
-21
module.ts
...ages/next/src/server/future/route-modules/pages/module.ts
+37
-11
route-module.ts
...ages/next/src/server/future/route-modules/route-module.ts
+0
-7
render-result.ts
packages/next/src/server/render-result.ts
+8
-2
render.tsx
packages/next/src/server/render.tsx
+4
-16
edge-route-module-wrapper.ts
packages/next/src/server/web/edge-route-module-wrapper.ts
+0
-4
next-request.ts
...xt/src/server/web/spec-extension/adapters/next-request.ts
+9
-5
index.js
test/integration/app-document-style-fragment/pages/index.js
+5
-1
No files found.
packages/next/src/server/base-server.ts
View file @
ab3f707e
...
@@ -11,11 +11,7 @@ import type { NextConfig, NextConfigComplete } from './config-shared'
...
@@ -11,11 +11,7 @@ import type { NextConfig, NextConfigComplete } from './config-shared'
import
type
{
NextParsedUrlQuery
,
NextUrlWithParsedQuery
}
from
'./request-meta'
import
type
{
NextParsedUrlQuery
,
NextUrlWithParsedQuery
}
from
'./request-meta'
import
type
{
ParsedUrlQuery
}
from
'querystring'
import
type
{
ParsedUrlQuery
}
from
'querystring'
import
type
{
RenderOpts
,
RenderOptsPartial
}
from
'./render'
import
type
{
RenderOpts
,
RenderOptsPartial
}
from
'./render'
import
type
{
import
type
{
ResponseCacheBase
,
ResponseCacheEntry
}
from
'./response-cache'
ResponseCacheBase
,
ResponseCacheEntry
,
ResponseCacheValue
,
}
from
'./response-cache'
import
type
{
UrlWithParsedQuery
}
from
'url'
import
type
{
UrlWithParsedQuery
}
from
'url'
import
{
import
{
NormalizeError
,
NormalizeError
,
...
@@ -1781,9 +1777,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...
@@ -1781,9 +1777,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
result
=
await
module
.
render
(
result
=
await
module
.
render
(
(
req
as
NodeNextRequest
).
originalRequest
,
(
req
as
NodeNextRequest
).
originalRequest
,
(
res
as
NodeNextResponse
).
originalResponse
,
(
res
as
NodeNextResponse
).
originalResponse
,
pathname
,
{
page
:
pathname
,
params
:
match
.
params
,
query
,
renderOpts
}
query
,
renderOpts
)
)
}
else
{
}
else
{
// If we didn't match a page, we should fallback to using the legacy
// If we didn't match a page, we should fallback to using the legacy
...
@@ -1835,24 +1829,41 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...
@@ -1835,24 +1829,41 @@ export default abstract class Server<ServerOptions extends Options = Options> {
throw
err
throw
err
}
}
let
value
:
ResponseCacheValue
|
null
// Based on the metadata, we can determine what kind of cache result we
// should return.
// Handle `isNotFound`.
if
(
metadata
.
isNotFound
)
{
if
(
metadata
.
isNotFound
)
{
value
=
null
return
{
value
:
null
,
revalidate
:
metadata
.
revalidate
}
}
else
if
(
metadata
.
isRedirect
)
{
}
value
=
{
kind
:
'REDIRECT'
,
props
:
metadata
.
pageData
}
}
else
{
// Handle `isRedirect`.
if
(
metadata
.
isRedirect
)
{
return
{
value
:
{
kind
:
'REDIRECT'
,
props
:
metadata
.
pageData
,
},
revalidate
:
metadata
.
revalidate
,
}
}
// Handle `isNull`.
if
(
result
.
isNull
())
{
if
(
result
.
isNull
())
{
return
null
return
{
value
:
null
,
revalidate
:
metadata
.
revalidate
}
}
}
value
=
{
// We now have a valid HTML result that we can return to the user.
return
{
value
:
{
kind
:
'PAGE'
,
kind
:
'PAGE'
,
html
:
result
,
html
:
result
,
pageData
:
metadata
.
pageData
,
pageData
:
metadata
.
pageData
,
headers
,
headers
,
},
revalidate
:
metadata
.
revalidate
,
}
}
}
}
return
{
revalidate
:
metadata
.
revalidate
,
value
}
}
const
cacheEntry
=
await
this
.
responseCache
.
get
(
const
cacheEntry
=
await
this
.
responseCache
.
get
(
ssgCacheKey
,
ssgCacheKey
,
...
...
packages/next/src/server/future/route-handler-managers/route-handler-manager.ts
View file @
ab3f707e
...
@@ -32,11 +32,6 @@ export class RouteHandlerManager {
...
@@ -32,11 +32,6 @@ export class RouteHandlerManager {
this
.
moduleLoader
this
.
moduleLoader
)
)
// Setup the handler. It is the responsibility of the module to ensure that
// this is only called once. If this is in development mode, the require
// cache will be cleared and the module will be re-created.
module
.
setup
()
// Convert the BaseNextRequest to a NextRequest.
// Convert the BaseNextRequest to a NextRequest.
const
request
=
NextRequestAdapter
.
fromBaseNextRequest
(
req
)
const
request
=
NextRequestAdapter
.
fromBaseNextRequest
(
req
)
...
...
packages/next/src/server/future/route-modules/app-route/module.ts
View file @
ab3f707e
...
@@ -187,27 +187,6 @@ export class AppRouteRouteModule extends RouteModule<
...
@@ -187,27 +187,6 @@ export class AppRouteRouteModule extends RouteModule<
)
)
}
}
}
}
}
/**
* When true, indicates that the global interfaces have been patched via the
* `patch()` method.
*/
private
hasSetup
:
boolean
=
false
/**
* Validates the userland module to ensure the exported methods and properties
* are valid.
*/
public
async
setup
()
{
// If we've already setup, then return.
if
(
this
.
hasSetup
)
return
// Mark the module as setup. The following warnings about the userland
// module will run if we're in development. If the module files are modified
// when in development, then the require cache will be busted for it and
// this method will be called again (resetting the `hasSetup` flag).
this
.
hasSetup
=
true
// We only warn in development after here, so return if we're not in
// We only warn in development after here, so return if we're not in
// development.
// development.
...
...
packages/next/src/server/future/route-modules/pages/module.ts
View file @
ab3f707e
...
@@ -11,7 +11,11 @@ import type { NextParsedUrlQuery } from '../../../request-meta'
...
@@ -11,7 +11,11 @@ import type { NextParsedUrlQuery } from '../../../request-meta'
import
type
{
RenderOpts
}
from
'../../../render'
import
type
{
RenderOpts
}
from
'../../../render'
import
type
RenderResult
from
'../../../render-result'
import
type
RenderResult
from
'../../../render-result'
import
{
RouteModule
,
type
RouteModuleOptions
}
from
'../route-module'
import
{
RouteModule
,
type
RouteModuleHandleContext
,
type
RouteModuleOptions
,
}
from
'../route-module'
import
{
renderToHTML
}
from
'../../../render'
import
{
renderToHTML
}
from
'../../../render'
/**
/**
...
@@ -46,6 +50,29 @@ export type PagesUserlandModule = {
...
@@ -46,6 +50,29 @@ export type PagesUserlandModule = {
readonly
getServerSideProps
?:
GetServerSideProps
readonly
getServerSideProps
?:
GetServerSideProps
}
}
/**
* AppRouteRouteHandlerContext is the context that is passed to the route
* handler for app routes.
*/
export
interface
PagesRouteHandlerContext
extends
RouteModuleHandleContext
{
/**
* The page for the given route.
*/
page
:
string
/**
* The parsed URL query for the given request.
*/
query
:
NextParsedUrlQuery
/**
* The RenderOpts for the given request which include the specific modules to
* use for rendering.
*/
// TODO: (wyattjoh) break this out into smaller parts, it currently includes the userland components
renderOpts
:
RenderOpts
}
export
type
PagesRouteModuleOptions
=
RouteModuleOptions
<
export
type
PagesRouteModuleOptions
=
RouteModuleOptions
<
PagesRouteDefinition
,
PagesRouteDefinition
,
PagesUserlandModule
PagesUserlandModule
...
@@ -55,23 +82,22 @@ export class PagesRouteModule extends RouteModule<
...
@@ -55,23 +82,22 @@ export class PagesRouteModule extends RouteModule<
PagesRouteDefinition
,
PagesRouteDefinition
,
PagesUserlandModule
PagesUserlandModule
>
{
>
{
public
setup
():
Promise
<
void
>
{
throw
new
Error
(
'Method not implemented.'
)
}
public
handle
():
Promise
<
Response
>
{
public
handle
():
Promise
<
Response
>
{
throw
new
Error
(
'Method not implemented.'
)
throw
new
Error
(
'Method not implemented.'
)
}
}
public
async
render
(
public
render
(
req
:
IncomingMessage
,
req
:
IncomingMessage
,
res
:
ServerResponse
,
res
:
ServerResponse
,
pathname
:
string
,
context
:
PagesRouteHandlerContext
query
:
NextParsedUrlQuery
,
renderOpts
:
RenderOpts
):
Promise
<
RenderResult
>
{
):
Promise
<
RenderResult
>
{
const
result
=
await
renderToHTML
(
req
,
res
,
pathname
,
query
,
renderOpts
)
return
renderToHTML
(
return
result
req
,
res
,
context
.
page
,
context
.
query
,
context
.
renderOpts
)
}
}
}
}
...
...
packages/next/src/server/future/route-modules/route-module.ts
View file @
ab3f707e
...
@@ -45,13 +45,6 @@ export abstract class RouteModule<
...
@@ -45,13 +45,6 @@ export abstract class RouteModule<
*/
*/
public
readonly
definition
:
Readonly
<
D
>
public
readonly
definition
:
Readonly
<
D
>
/**
* Setup will setup the route handler. This could patch any globals or perform
* validation of the userland module. It is the responsibility of the module
* to ensure that this is only called once.
*/
public
abstract
setup
():
Promise
<
void
>
/**
/**
* Handle will handle the request and return a response.
* Handle will handle the request and return a response.
*/
*/
...
...
packages/next/src/server/render-result.ts
View file @
ab3f707e
...
@@ -12,13 +12,15 @@ export type RenderResultMetadata = {
...
@@ -12,13 +12,15 @@ export type RenderResultMetadata = {
isRedirect
?:
boolean
isRedirect
?:
boolean
}
}
type
RenderResultResponse
=
string
|
ReadableStream
<
Uint8Array
>
|
null
export
default
class
RenderResult
{
export
default
class
RenderResult
{
private
readonly
_response
:
string
|
ReadableStream
<
Uint8Array
>
|
null
private
readonly
_response
:
RenderResultResponse
private
readonly
_contentType
:
ContentTypeOption
private
readonly
_contentType
:
ContentTypeOption
private
readonly
_metadata
:
RenderResultMetadata
private
readonly
_metadata
:
RenderResultMetadata
constructor
(
constructor
(
response
:
string
|
ReadableStream
<
Uint8Array
>
|
null
,
response
:
RenderResultResponse
,
{
{
contentType
,
contentType
,
...
metadata
...
metadata
...
@@ -31,6 +33,10 @@ export default class RenderResult {
...
@@ -31,6 +33,10 @@ export default class RenderResult {
this
.
_metadata
=
metadata
this
.
_metadata
=
metadata
}
}
get
body
():
Readonly
<
RenderResultResponse
>
{
return
this
.
_response
}
get
metadata
():
Readonly
<
RenderResultMetadata
>
{
get
metadata
():
Readonly
<
RenderResultMetadata
>
{
return
this
.
_metadata
return
this
.
_metadata
}
}
...
...
packages/next/src/server/render.tsx
View file @
ab3f707e
...
@@ -75,7 +75,6 @@ import {
...
@@ -75,7 +75,6 @@ import {
streamFromArray
,
streamFromArray
,
streamToString
,
streamToString
,
chainStreams
,
chainStreams
,
createBufferedTransformStream
,
renderToInitialStream
,
renderToInitialStream
,
continueFromInitialStream
,
continueFromInitialStream
,
}
from
'./stream-utils/node-web-streams-helper'
}
from
'./stream-utils/node-web-streams-helper'
...
@@ -1176,8 +1175,6 @@ export async function renderToHTML(
...
@@ -1176,8 +1175,6 @@ export async function renderToHTML(
return
inAmpMode
?
children
:
<
div
id=
"__next"
>
{
children
}
</
div
>
return
inAmpMode
?
children
:
<
div
id=
"__next"
>
{
children
}
</
div
>
}
}
// Always disable streaming for pages rendering
const
generateStaticHTML
=
true
const
renderDocument
=
async
()
=>
{
const
renderDocument
=
async
()
=>
{
// For `Document`, there are two cases that we don't support:
// For `Document`, there are two cases that we don't support:
// 1. Using `Document.getInitialProps` in the Edge runtime.
// 1. Using `Document.getInitialProps` in the Edge runtime.
...
@@ -1315,7 +1312,7 @@ export async function renderToHTML(
...
@@ -1315,7 +1312,7 @@ export async function renderToHTML(
return
continueFromInitialStream
(
initialStream
,
{
return
continueFromInitialStream
(
initialStream
,
{
suffix
,
suffix
,
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
generateStaticHTML
,
generateStaticHTML
:
true
,
getServerInsertedHTML
,
getServerInsertedHTML
,
serverInsertedHTMLToHead
:
false
,
serverInsertedHTMLToHead
:
false
,
})
})
...
@@ -1538,18 +1535,9 @@ export async function renderToHTML(
...
@@ -1538,18 +1535,9 @@ export async function renderToHTML(
const
postOptimize
=
(
html
:
string
)
=>
const
postOptimize
=
(
html
:
string
)
=>
postProcessHTML
(
pathname
,
html
,
renderOpts
,
{
inAmpMode
,
hybridAmp
})
postProcessHTML
(
pathname
,
html
,
renderOpts
,
{
inAmpMode
,
hybridAmp
})
if
(
generateStaticHTML
)
{
const
html
=
await
streamToString
(
chainStreams
(
streams
))
const
html
=
await
streamToString
(
chainStreams
(
streams
))
const
optimizedHtml
=
await
postOptimize
(
html
)
const
optimizedHtml
=
await
postOptimize
(
html
)
return
new
RenderResult
(
optimizedHtml
,
renderResultMeta
)
return
new
RenderResult
(
optimizedHtml
,
renderResultMeta
)
}
return
new
RenderResult
(
chainStreams
(
streams
).
pipeThrough
(
createBufferedTransformStream
(
postOptimize
)
),
renderResultMeta
)
}
}
export
type
RenderToHTMLResult
=
typeof
renderToHTML
export
type
RenderToHTMLResult
=
typeof
renderToHTML
packages/next/src/server/web/edge-route-module-wrapper.ts
View file @
ab3f707e
...
@@ -62,10 +62,6 @@ export class EdgeRouteModuleWrapper {
...
@@ -62,10 +62,6 @@ export class EdgeRouteModuleWrapper {
}
}
private
async
handler
(
request
:
NextRequest
):
Promise
<
Response
>
{
private
async
handler
(
request
:
NextRequest
):
Promise
<
Response
>
{
// Setup the handler if it hasn't been setup yet. It is the responsibility
// of the module to ensure that this is only called once.
this
.
routeModule
.
setup
()
// Get the pathname for the matcher. Pathnames should not have trailing
// Get the pathname for the matcher. Pathnames should not have trailing
// slashes for matching.
// slashes for matching.
const
pathname
=
removeTrailingSlash
(
new
URL
(
request
.
url
).
pathname
)
const
pathname
=
removeTrailingSlash
(
new
URL
(
request
.
url
).
pathname
)
...
...
packages/next/src/server/web/spec-extension/adapters/next-request.ts
View file @
ab3f707e
...
@@ -8,8 +8,7 @@ import { NextRequest } from '../request'
...
@@ -8,8 +8,7 @@ import { NextRequest } from '../request'
export
class
NextRequestAdapter
{
export
class
NextRequestAdapter
{
public
static
fromBaseNextRequest
(
request
:
BaseNextRequest
):
NextRequest
{
public
static
fromBaseNextRequest
(
request
:
BaseNextRequest
):
NextRequest
{
// TODO: look at refining this check
if
(
request
.
constructor
.
name
===
'WebNextRequest'
)
{
if
(
'request'
in
request
&&
(
request
as
WebNextRequest
).
request
)
{
return
NextRequestAdapter
.
fromWebNextRequest
(
request
as
WebNextRequest
)
return
NextRequestAdapter
.
fromWebNextRequest
(
request
as
WebNextRequest
)
}
}
...
@@ -30,10 +29,15 @@ export class NextRequestAdapter {
...
@@ -30,10 +29,15 @@ export class NextRequestAdapter {
}
else
{
}
else
{
// Grab the full URL from the request metadata.
// Grab the full URL from the request metadata.
const
base
=
getRequestMeta
(
request
,
'__NEXT_INIT_URL'
)
const
base
=
getRequestMeta
(
request
,
'__NEXT_INIT_URL'
)
if
(
!
base
)
throw
new
Error
(
'Invariant: missing url on request'
)
if
(
!
base
||
!
base
.
startsWith
(
'http'
))
{
// Because the URL construction relies on the fact that the URL provided
// is absolute, we need to provide a base URL. We can't use the request
// URL because it's relative, so we use a dummy URL instead.
url
=
new
URL
(
request
.
url
,
'http://n'
)
}
else
{
url
=
new
URL
(
request
.
url
,
base
)
url
=
new
URL
(
request
.
url
,
base
)
}
}
}
return
new
NextRequest
(
url
,
{
return
new
NextRequest
(
url
,
{
body
,
body
,
...
...
test/integration/app-document-style-fragment/pages/index.js
View file @
ab3f707e
...
@@ -11,6 +11,10 @@ function Hi() {
...
@@ -11,6 +11,10 @@ function Hi() {
)
)
}
}
Hi
.
getInitialProps
=
()
=>
({})
Hi
.
getInitialProps
=
()
=>
({
// To prevent the warning related to an empty object from getInitialProps, we
// need to return something.
foo
:
'bar'
,
})
export
default
Hi
export
default
Hi
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment