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: support for Next.js 15 #6524

Merged
merged 13 commits into from
Jan 13, 2025
2 changes: 1 addition & 1 deletion examples/react/next-app-router/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
2 changes: 1 addition & 1 deletion examples/react/next-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"algoliasearch": "5.1.1",
"instantsearch.css": "8.5.1",
"next": "13.5.1",
"next": "15.1.4",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-instantsearch": "7.13.10",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/next-routing/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
2 changes: 1 addition & 1 deletion examples/react/next-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"algoliasearch": "5.1.1",
"instantsearch.css": "8.5.1",
"next": "13.5.1",
"next": "15.1.4",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-instantsearch": "7.13.10",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/next/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
2 changes: 1 addition & 1 deletion examples/react/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"algoliasearch": "5.1.1",
"instantsearch.css": "8.5.1",
"next": "13.5.1",
"next": "15.1.4",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-instantsearch": "7.13.10",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-instantsearch-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
},
"devDependencies": {
"instantsearch.js": "4.75.7",
"next": "13.5.1",
"next": "15.1.4",
"react-instantsearch-core": "7.13.10"
},
"peerDependencies": {
"next": ">= 13.4 < 15",
"next": ">= 13.4 < 16",
shaejaz marked this conversation as resolved.
Show resolved Hide resolved
"react-instantsearch": ">= 7.1.0 < 8"
}
}
6 changes: 4 additions & 2 deletions packages/react-instantsearch-nextjs/src/InstantSearchNext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { safelyRunOnBrowser } from 'instantsearch.js/es/lib/utils';
import { headers } from 'next/headers';
import React, { useEffect, useMemo, useRef } from 'react';
import {
InstantSearch,
Expand All @@ -10,6 +9,7 @@ import {
import { InitializePromise } from './InitializePromise';
import { TriggerSearch } from './TriggerSearch';
import { useInstantSearchRouting } from './useInstantSearchRouting';
import { useNextHeaders } from './useNextHeaders';
import { warn } from './warn';

import type { InitialResults, StateMapping, UiState } from 'instantsearch.js';
Expand Down Expand Up @@ -72,8 +72,10 @@ export function InstantSearchNext<
};
}, []);

const headers = useNextHeaders();

const nonce = safelyRunOnBrowser(() => undefined, {
fallback: () => headers().get('x-nonce') || undefined,
fallback: () => headers?.get('x-nonce') || undefined,
});

const routing = useInstantSearchRouting(passedRouting, isMounting);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import historyRouter from 'instantsearch.js/es/lib/routers/history';
import { headers } from 'next/headers';
import { usePathname, useSearchParams } from 'next/navigation';
import { useRef, useEffect } from 'react';

import { useNextHeaders } from './useNextHeaders';

import type { InstantSearchNextProps } from './InstantSearchNext';
import type { UiState } from 'instantsearch.js';
import type { BrowserHistoryArgs } from 'instantsearch.js/es/lib/routers/history';
Expand All @@ -15,6 +16,7 @@ export function useInstantSearchRouting<
passedRouting: InstantSearchNextProps<TUiState, TRouteState>['routing'],
isMounting: React.RefObject<boolean>
) {
const isServer = typeof window === 'undefined';
const pathname = usePathname();
const searchParams = useSearchParams();
const routingRef =
Expand All @@ -26,14 +28,16 @@ export function useInstantSearchRouting<
}
}, [pathname, searchParams]);

const headers = useNextHeaders();

if (passedRouting && !routingRef.current) {
let browserHistoryOptions: Partial<BrowserHistoryArgs<TRouteState>> = {};

browserHistoryOptions.getLocation = () => {
if (typeof window === 'undefined') {
if (isServer) {
const url = `${
headers().get('x-forwarded-proto') || 'http'
}://${headers().get('host')}${pathname}?${searchParams}`;
headers?.get('x-forwarded-proto') || 'http'
}://${headers?.get('host')}${pathname}?${searchParams}`;
return new URL(url) as unknown as Location;
}

Expand Down
29 changes: 29 additions & 0 deletions packages/react-instantsearch-nextjs/src/useNextHeaders.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { headers } from 'next/headers';
import { use } from 'react';

function isPromise(obj: any) {
return (
obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
}

type Headers = Awaited<ReturnType<typeof headers>> | undefined;
dhayab marked this conversation as resolved.
Show resolved Hide resolved

export const useNextHeaders = () => {
const isServer = typeof window === 'undefined';

let h: Headers;

if (isServer) {
const nextHeaders = headers();
if (isPromise(headers())) {
shaejaz marked this conversation as resolved.
Show resolved Hide resolved
h = use(nextHeaders);
} else {
h = nextHeaders as unknown as Headers; // assert that headers come from the synchronous nextjs function
}
}

return h;
};
2 changes: 1 addition & 1 deletion packages/react-instantsearch-router-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@
"ts-node": "8.4.1"
},
"peerDependencies": {
"next": ">= 9 && < 15"
"next": ">= 9 && < 16"
}
}
Loading