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

fix: fix history pushState/replaceState rate limits #33

Merged
merged 1 commit into from
Nov 20, 2024
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
4 changes: 4 additions & 0 deletions packages/urlstate/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { isSafari } from "../utils";
asmyshlyaev177 marked this conversation as resolved.
Show resolved Hide resolved

export const SYMBOLS = {
date: "⏲",
undefined: "∙undefined",
} as const;

export const TIMEOUT = isSafari ? 330 : 70;
2 changes: 1 addition & 1 deletion packages/urlstate/next/useUrlState/useUrlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function useUrlState<T extends JSONCompatible>(
reset: resetBase,
getState,
} = useUrlStateBase(_defaultState, router, ({ parse }) =>
isSSR()
isSSR
? parseSPObj(
filterUnknownParams(_defaultState, _searchParams),
_defaultState,
Expand Down
16 changes: 6 additions & 10 deletions packages/urlstate/useSharedState/useSharedState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import { act, renderHook } from '@testing-library/react';

import { useSharedState } from './useSharedState';
import * as subscribers from '../subscribers';
import * as utils from '../utils'

jest.mock('../utils.ts', () => ({
...jest.requireActual('../utils.ts'),
isSSR: jest.fn(),
}));

import { isSSR, JSONCompatible } from '../utils';
import { JSONCompatible } from '../utils';

describe('useSharedState', () => {
let state: typeof form;
Expand All @@ -27,7 +23,7 @@ describe('useSharedState', () => {
it('always use getInitial', () => {
const stateSpy = jest.spyOn(subscribers.stateMap, 'get');
const stateSpySet = jest.spyOn(subscribers.stateMap, 'set');
jest.mocked(isSSR).mockReturnValue(true);
Object.defineProperty(utils, 'isSSR', { value: true })
const hook1 = renderHook(() => useSharedState(state, getInitial));

expect(hook1.result.current.state).toStrictEqual(initial);
Expand All @@ -43,7 +39,7 @@ describe('useSharedState', () => {
.spyOn(subscribers.stateMap, 'set')
.mockReturnValue(stateMock);

jest.mocked(isSSR).mockReturnValue(false);
Object.defineProperty(utils, 'isSSR', { value: false })
const hook1 = renderHook(() => useSharedState(state, getInitial));

expect(hook1.result.current.state).toStrictEqual(initial);
Expand All @@ -61,7 +57,7 @@ describe('useSharedState', () => {
.spyOn(subscribers.stateMap, 'set')
.mockReturnValue(stateMock);

jest.mocked(isSSR).mockReturnValue(false);
Object.defineProperty(utils, 'isSSR', { value: false })
const hook1 = renderHook(() => useSharedState(state));

expect(stateSpyGet).toHaveBeenCalledTimes(1);
Expand All @@ -80,7 +76,7 @@ describe('useSharedState', () => {
.spyOn(subscribers.stateMap, 'set')
.mockReturnValue(stateMock);

jest.mocked(isSSR).mockReturnValue(false);
Object.defineProperty(utils, 'isSSR', { value: false })
const hook1 = renderHook(() => useSharedState(state));

expect(stateSpyGet).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/urlstate/useSharedState/useSharedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useSharedState<T extends JSONCompatible>(
const stateShape = React.useRef(defaultState);

const [state, _setState] = React.useState(() => {
if (isSSR()) {
if (isSSR) {
return _getInitial ? _getInitial?.() : stateShape.current;
}

Expand Down
Loading