-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #826 from Zexron-xu/feature/extract-to-json
feat(ssr): support extract to json and restore state
- Loading branch information
Showing
6 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { restoreState } from '../browser' | ||
|
||
beforeEach(() => { | ||
// @ts-expect-error | ||
global.document = {} | ||
// @ts-expect-error | ||
global.window = {} | ||
}) | ||
|
||
afterEach(() => { | ||
// @ts-expect-error | ||
delete global.document | ||
// @ts-expect-error | ||
delete global.window | ||
}) | ||
|
||
describe('Browser function test', () => { | ||
it('should add data into window', () => { | ||
global.document.getElementById = jest.fn().mockReturnValue({ textContent: '{}' }) | ||
restoreState() | ||
expect(global.window['SIGI_STATE']).toEqual({}) | ||
expect(global.window['SIGI_RETRY']).toEqual({}) | ||
}) | ||
|
||
it('should not add data into window', () => { | ||
global.document.getElementById = jest.fn().mockReturnValue(null) | ||
restoreState() | ||
expect(global.window['SIGI_STATE']).toEqual(undefined) | ||
expect(global.window['SIGI_RETRY']).toEqual(undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { GLOBAL_KEY_SYMBOL, RETRY_KEY_SYMBOL } from '@sigi/core' | ||
|
||
export function restoreState() { | ||
const sigiStateContent = document.getElementById(GLOBAL_KEY_SYMBOL)?.textContent | ||
const sigiRetryContent = document.getElementById(RETRY_KEY_SYMBOL)?.textContent | ||
|
||
if (sigiStateContent) { | ||
window[GLOBAL_KEY_SYMBOL] = JSON.parse(sigiStateContent) | ||
} | ||
|
||
if (sigiRetryContent) { | ||
window[RETRY_KEY_SYMBOL] = JSON.parse(sigiRetryContent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { runSSREffects as emitSSREffects, ModuleMeta } from './run' | ||
export { match } from './match' | ||
export { restoreState } from './browser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters