Skip to content

Commit

Permalink
fixes the logging by showing full URLs only on demand (#58088)
Browse files Browse the repository at this point in the history
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #58087

-->

fixes #58087

Currently in Next 14, everyone has fullURL flag turned to true, this PR
reverts the condition.

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
  • Loading branch information
raphaelbadia and huozhi authored Dec 7, 2023
1 parent e1fe0c9 commit d07a370
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
11 changes: 0 additions & 11 deletions packages/next/src/build/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,7 @@ export function watchCompilers(
})
}

const internalSegments = ['[[...__metadata_id__]]', '[__metadata_id__]']
export function reportTrigger(trigger: string, url?: string) {
for (const segment of internalSegments) {
if (trigger.includes(segment)) {
trigger = trigger.replace(segment, '')
}
}

if (trigger.length > 1 && trigger.endsWith('/')) {
trigger = trigger.slice(0, -1)
}

buildStore.setState({
trigger,
url,
Expand Down
16 changes: 15 additions & 1 deletion packages/next/src/build/output/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export type OutputState =
}
))

const internalSegments = ['[[...__metadata_id__]]', '[__metadata_id__]']
export function formatTrigger(trigger: string) {
for (const segment of internalSegments) {
if (trigger.includes(segment)) {
trigger = trigger.replace(segment, '')
}
}
if (trigger.length > 1 && trigger.endsWith('/')) {
trigger = trigger.slice(0, -1)
}
return trigger
}

export const store = createStore<OutputState>({
appUrl: null,
bindAddr: null,
Expand Down Expand Up @@ -67,7 +80,8 @@ store.subscribe((state) => {

if (state.loading) {
if (state.trigger) {
trigger = state.trigger
trigger = formatTrigger(state.trigger)
console.log('trigger', trigger)
triggerUrl = state.url
if (trigger !== 'initial') {
traceSpan = trace('compile-path', undefined, {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ export default class NextNodeServer extends BaseServer {

const loggingFetchesConfig = this.nextConfig.logging?.fetches
const enabledVerboseLogging = !!loggingFetchesConfig
const shouldTruncateUrl = loggingFetchesConfig?.fullUrl
const shouldTruncateUrl = !loggingFetchesConfig?.fullUrl

if (this.renderOpts.dev) {
const { bold, green, yellow, red, gray, white } =
Expand Down
46 changes: 41 additions & 5 deletions test/e2e/app-dir/logging/fetch-logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path'
import fs from 'fs'
import stripAnsi from 'strip-ansi'
import { check } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
Expand Down Expand Up @@ -42,7 +44,13 @@ createNextDescribe(
files: __dirname,
},
({ next, isNextDev }) => {
function runTests({ withFetchesLogging }: { withFetchesLogging: boolean }) {
function runTests({
withFetchesLogging,
withFullUrlFetches = false,
}: {
withFetchesLogging: boolean
withFullUrlFetches?: boolean
}) {
if (withFetchesLogging) {
it('should only log requests in dev mode', async () => {
const outputIndex = next.cliOutput.length
Expand Down Expand Up @@ -74,8 +82,9 @@ createNextDescribe(
log.url.includes('api/random?no-cache')
)

// expend full url
expect(logs.every((log) => log.url.includes('..'))).toBe(false)
expect(logs.some((log) => log.url.includes('..'))).toBe(
!withFullUrlFetches
)

if (logEntry?.cache === 'cache: no-cache') {
return 'success'
Expand Down Expand Up @@ -184,8 +193,28 @@ createNextDescribe(
}
}

describe('with verbose logging', () => {
runTests({ withFetchesLogging: true })
describe('with fetches verbose logging', () => {
runTests({ withFetchesLogging: true, withFullUrlFetches: true })
})

describe('with fetches default logging', () => {
const curNextConfig = fs.readFileSync(
path.join(__dirname, 'next.config.js'),
{ encoding: 'utf-8' }
)
beforeAll(async () => {
await next.stop()
await next.patchFile(
'next.config.js',
curNextConfig.replace('fullUrl: true', 'fullUrl: false')
)
await next.start()
})
afterAll(async () => {
await next.patchFile('next.config.js', curNextConfig)
})

runTests({ withFetchesLogging: true, withFullUrlFetches: false })
})

describe('with verbose logging for edge runtime', () => {
Expand All @@ -203,11 +232,18 @@ createNextDescribe(
})

describe('with default logging', () => {
const curNextConfig = fs.readFileSync(
path.join(__dirname, 'next.config.js'),
{ encoding: 'utf-8' }
)
beforeAll(async () => {
await next.stop()
await next.deleteFile('next.config.js')
await next.start()
})
afterAll(async () => {
await next.patchFile('next.config.js', curNextConfig)
})

runTests({ withFetchesLogging: false })
})
Expand Down

0 comments on commit d07a370

Please sign in to comment.