Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow users to customize fetch behavior #1599

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/__tests__/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ describe('request', () => {
})
})

it('supports nextOptions parameter', async () => {
request(
createRequest({
fetchOptions: { cache: 'force-cache', next: { revalidate: 0, tags: ['test'] } },
})
)
await flushPromises()

expect(mockedFetch).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
cache: 'force-cache',
next: { revalidate: 0, tags: ['test'] },
})
)
})

describe('keepalive with fetch and large bodies can cause some browsers to reject network calls', () => {
it.each([
['always keepalive with small json POST', 'POST', 'small', undefined, true, ''],
Expand Down
4 changes: 4 additions & 0 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ export class PostHog {
}
options.compression = options.compression === 'best-available' ? this.compression : options.compression

// Specially useful if you're doing SSR with NextJS
// Users must be careful when tweaking `cache` because they might get out-of-date feature flags
options.fetchOptions = options.fetchOptions || this.config.fetch_options

request({
...options,
callback: (response) => {
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const _fetch = (options: RequestOptions) => {
keepalive: options.method === 'POST' && (estimatedSize || 0) < KEEP_ALIVE_THRESHOLD,
body,
signal: aborter?.signal,
...options.fetchOptions,
})
.then((response) => {
return response.text().then((responseText) => {
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ export interface PostHogConfig {
events_burst_limit?: number
}

/** Used when sending data via `fetch`, use with care, this is intentionally meant to be used with NextJS `fetch`
* Incorrect usage may cause out-of-date data for feature flags, actions tracking, etc.
* See https://nextjs.org/docs/app/api-reference/functions/fetch#fetchurl-options
*/
fetch_options?: {
cache?: RequestInit['cache']
next_options?: NextOptions
}

/**
* PREVIEW - MAY CHANGE WITHOUT WARNING - DO NOT USE IN PRODUCTION
* whether to wrap fetch and add tracing headers to the request
Expand Down Expand Up @@ -440,6 +449,9 @@ export interface RequestResponse {

export type RequestCallback = (response: RequestResponse) => void

// See https://nextjs.org/docs/app/api-reference/functions/fetch#fetchurl-options
type NextOptions = { revalidate: false | 0 | number; tags: string[] }

export interface RequestOptions {
url: string
// Data can be a single object or an array of objects when batched
Expand All @@ -452,6 +464,10 @@ export interface RequestOptions {
timeout?: number
noRetries?: boolean
compression?: Compression | 'best-available'
fetchOptions?: {
cache?: RequestInit['cache']
next?: NextOptions
}
}

// Queued request types - the same as a request but with additional queueing information
Expand Down
Loading