Skip to content

Commit

Permalink
Merge pull request #33 from asmyshlyaev177/fix_history_rate_limits
Browse files Browse the repository at this point in the history
fix: fix history pushState/replaceState rate limits
  • Loading branch information
asmyshlyaev177 authored Nov 20, 2024
2 parents 49b67cd + 35d1398 commit 9b7f5f9
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 50 deletions.
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";

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

0 comments on commit 9b7f5f9

Please sign in to comment.