Skip to content

Commit

Permalink
update nextjs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shaejaz committed Jan 13, 2025
1 parent 86a33c7 commit a238848
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 124 deletions.
2 changes: 1 addition & 1 deletion examples/react/next-app-router/lib/client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMemoryCache } from '@algolia/client-common';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { liteClient as algoliasearch } from 'algoliasearch-v5/lite';

export const responsesCache = createMemoryCache();
export const client = algoliasearch(
Expand Down
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.
5 changes: 2 additions & 3 deletions examples/react/next-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
"dependencies": {
"algoliasearch": "5.1.1",
"instantsearch.css": "8.5.1",
"next": "13.5.1",
"next": "^15.0.0",
"react": "19.0.0",
"react-dom": "19.0.0",
"react-instantsearch": "7.13.10",
"react-instantsearch-nextjs": "0.3.21"
"react-instantsearch": "7.13.10"
},
"devDependencies": {
"@types/node": "17.0.40",
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.0.0",
"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/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMemoryCache } from '@algolia/client-common';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { liteClient as algoliasearch } from 'algoliasearch-v5/lite';
import { Hit as AlgoliaHit } from 'instantsearch.js';
import { GetServerSideProps } from 'next';
import Head from 'next/head';
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.0.0",
"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/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMemoryCache } from '@algolia/client-common';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { liteClient as algoliasearch } from 'algoliasearch-v5/lite';
import { Hit as AlgoliaHit } from 'instantsearch.js';
import { GetServerSideProps } from 'next';
import Head from 'next/head';
Expand Down
1 change: 1 addition & 0 deletions packages/react-instantsearch-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ export * from './hooks/useInstantSearch';
export * from './lib/wrapPromiseWithState';
export * from './lib/useInstantSearchContext';
export * from './lib/useRSCContext';
export * from './lib/use';
export * from './lib/InstantSearchRSCContext';
export * from './server';
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.0.0",

Check warning on line 53 in packages/react-instantsearch-nextjs/package.json

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/react-instantsearch-nextjs/package.json#L53

Package dependencies with variant versions may lead to dependency hijack and confusion attacks.
"react-instantsearch-core": "7.13.10"
},
"peerDependencies": {
"next": ">= 13.4 < 15",
"next": ">= 13.4 < 16",
"react-instantsearch": ">= 7.1.0 < 8"
}
}
13 changes: 9 additions & 4 deletions packages/react-instantsearch-nextjs/src/InstantSearchNext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { safelyRunOnBrowser } from 'instantsearch.js/es/lib/utils';
import { headers } from 'next/headers';
import React, { useEffect, useMemo, useRef } from 'react';
import React, { use, useEffect, useMemo, useRef } from 'react';
import {
InstantSearch,
InstantSearchRSCContext,
Expand Down Expand Up @@ -72,9 +72,14 @@ export function InstantSearchNext<
};
}, []);

const nonce = safelyRunOnBrowser(() => undefined, {
fallback: () => headers().get('x-nonce') || undefined,
});
let nonce: string | undefined;
if (isServer) {
const h = use(headers());

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

const routing = useInstantSearchRouting(passedRouting, isMounting);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ jest.mock('next/navigation', () => ({
},
}));

// const mockHeaders = jest.fn(() => Promise.resolve({}));
jest.mock('next/headers', () => ({
...jest.requireActual('next/headers'),
headers: () => Promise.resolve({}),
}));

describe('rerendering', () => {
const client = createSearchClient();

Expand Down
17 changes: 12 additions & 5 deletions packages/react-instantsearch-nextjs/src/useInstantSearchRouting.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import historyRouter from 'instantsearch.js/es/lib/routers/history';
import { headers } from 'next/headers';
import { headers as nextHeaders } from 'next/headers';
import { usePathname, useSearchParams } from 'next/navigation';
import { useRef, useEffect } from 'react';
import { useRef, useEffect, use } from 'react';

import type { InstantSearchNextProps } from './InstantSearchNext';
import type { UiState } from 'instantsearch.js';
import type { BrowserHistoryArgs } from 'instantsearch.js/es/lib/routers/history';
import type { ReadonlyHeaders } from 'next/dist/server/web/spec-extension/adapters/headers';
import type { InstantSearchProps } from 'react-instantsearch-core';

export function useInstantSearchRouting<
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,19 @@ export function useInstantSearchRouting<
}
}, [pathname, searchParams]);

let headers: ReadonlyHeaders | undefined;
if (isServer) {
headers = use(nextHeaders());
}

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
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

0 comments on commit a238848

Please sign in to comment.