Skip to content

Commit

Permalink
Merge branch 'canary' into add-env-var-info-to-middleware-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle authored Nov 22, 2021
2 parents c2ceeaf + f584722 commit 9fdad0a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { NextRequest } from '../../../../server/web/spec-extension/request'
import { renderToHTML } from '../../../../server/web/render'
import RenderResult from '../../../../server/render-result'

const createHeaders = (args?: any) => ({
...args,
'x-middleware-ssr': '1',
})

export function getRender({
App,
Document,
Expand All @@ -24,15 +29,15 @@ export function getRender({
restRenderOpts: any
}) {
return async function render(request: NextRequest) {
const url = request.nextUrl
const { nextUrl: url, cookies, headers } = request
const { pathname, searchParams } = url

const query = Object.fromEntries(searchParams)

// Preflight request
if (request.method === 'HEAD') {
return new Response('OK.', {
headers: { 'x-middleware-ssr': '1' },
return new Response(null, {
headers: createHeaders(),
})
}

Expand All @@ -41,7 +46,11 @@ export function getRender({
: false
delete query.__flight__

const req = { url: pathname }
const req = {
url: pathname,
cookies,
headers,
}
const renderOpts = {
...restRenderOpts,
// Locales are not supported yet.
Expand Down Expand Up @@ -103,7 +112,7 @@ export function getRender({
).toString(),
{
status: 500,
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
}
)
}
Expand All @@ -114,7 +123,7 @@ export function getRender({
'An error occurred while rendering ' + pathname + '.',
{
status: 500,
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
}
)
}
Expand All @@ -126,7 +135,7 @@ export function getRender({
} as any)

return new Response(transformStream.readable, {
headers: { 'x-middleware-ssr': '1' },
headers: createHeaders(),
status: 200,
})
}
Expand Down
7 changes: 4 additions & 3 deletions packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,13 +688,14 @@ if (process.env.__NEXT_RSC) {
}

RSCComponent = (props: any) => {
const { asPath: cacheKey } = useRouter() as any
const cacheKey = useRouter().asPath
const { __flight_serialized__, __flight_fresh__ } = props
return (
<React.Suspense fallback={null}>
<RSCWrapper
cacheKey={cacheKey}
serialized={(props as any).__flight_serialized__}
_fresh={(props as any).__flight_fresh__}
serialized={__flight_serialized__}
_fresh={__flight_fresh__}
/>
</React.Suspense>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ export async function renderToHTML(

if (renderServerComponentData) {
const stream: ReadableStream = renderToReadableStream(
<OriginalComponent {...props} />,
<OriginalComponent {...props.pageProps} />,
serverComponentManifest
)
const reader = stream.getReader()
Expand Down
30 changes: 2 additions & 28 deletions packages/next/trace/report/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { TARGET, SpanId } from '../shared'
import reportToConsole from './to-console'
import reportToZipkin from './to-zipkin'
import reportToJaeger from './to-jaeger'
import { SpanId } from '../shared'
import reportToTelemetry from './to-telemetry'
import reportToJson from './to-json'

Expand Down Expand Up @@ -42,28 +39,5 @@ class MultiReporter implements Reporter {
}
}

const target =
process.env.TRACE_TARGET && process.env.TRACE_TARGET in TARGET
? TARGET[process.env.TRACE_TARGET as TARGET]
: TARGET.TELEMETRY

if (process.env.TRACE_TARGET && !target) {
console.info(
'For TRACE_TARGET, please specify one of: CONSOLE, ZIPKIN, TELEMETRY'
)
}

let traceTargetReporter: Reporter

if (target === TARGET.CONSOLE) {
traceTargetReporter = reportToConsole
} else if (target === TARGET.ZIPKIN) {
traceTargetReporter = reportToZipkin
} else if (target === TARGET.JAEGER) {
traceTargetReporter = reportToJaeger
} else {
traceTargetReporter = reportToTelemetry
}

// JSON is always reported to allow for diagnostics
export const reporter = new MultiReporter([reportToJson, traceTargetReporter])
export const reporter = new MultiReporter([reportToJson, reportToTelemetry])
32 changes: 0 additions & 32 deletions packages/next/trace/report/to-console.ts

This file was deleted.

83 changes: 0 additions & 83 deletions packages/next/trace/report/to-jaeger.ts

This file was deleted.

45 changes: 44 additions & 1 deletion packages/next/trace/report/to-json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
import { randomBytes } from 'crypto'
import { batcher } from './to-zipkin'
import { traceGlobals } from '../shared'
import fs from 'fs'
import path from 'path'
import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants'

const localEndpoint = {
serviceName: 'nextjs',
ipv4: '127.0.0.1',
port: 9411,
}

type Event = {
traceId: string
parentId?: string
name: string
id: string
timestamp: number
duration: number
localEndpoint?: typeof localEndpoint
tags?: Object
}

// Batch events as zipkin allows for multiple events to be sent in one go
export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
const events: Event[] = []
// Promise queue to ensure events are always sent on flushAll
const queue = new Set()
return {
flushAll: async () => {
await Promise.all(queue)
if (events.length > 0) {
await reportEvents(events)
events.length = 0
}
},
report: (event: Event) => {
events.push(event)

if (events.length > 100) {
const evts = events.slice()
events.length = 0
const report = reportEvents(evts)
queue.add(report)
report.then(() => queue.delete(report))
}
},
}
}

let writeStream: RotatingWriteStream
let traceId: string
let batch: ReturnType<typeof batcher> | undefined
Expand Down
Loading

0 comments on commit 9fdad0a

Please sign in to comment.