-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Service worker implementation #5168
Draft
andrzejewsky
wants to merge
6
commits into
main
Choose a base branch
from
merx-981-service-worker-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca6205f
SW implementation
andrzejewsky 39f66cf
SW implementation
andrzejewsky 68b3f18
SW implementation
andrzejewsky b37b7e4
SW implementation
andrzejewsky ceca51b
SW implementation
andrzejewsky 42c2ede
SW implementation
andrzejewsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Now you can see when the new version was released or you lost connection. |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,45 @@ | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
|
||
import { Animated } from "./Animated"; | ||
|
||
describe("Animated Component", () => { | ||
test("renders children when show is true", () => { | ||
// Arrange & Act | ||
render(<Animated show={true}>Test Child</Animated>); | ||
|
||
// Assert | ||
expect(screen.getByText("Test Child")).toBeInTheDocument(); | ||
}); | ||
|
||
test("does not render children when show is false", () => { | ||
// Arrange & Act | ||
const { rerender } = render(<Animated show={false}>Test Child</Animated>); | ||
|
||
// Assert | ||
expect(screen.queryByText("Test Child")).not.toBeInTheDocument(); | ||
|
||
// Arrange & Act | ||
rerender(<Animated show={true}>Test Child</Animated>); | ||
|
||
// Assert | ||
expect(screen.getByText("Test Child")).toBeInTheDocument(); | ||
}); | ||
|
||
test("handles transition end correctly", () => { | ||
// Arrange & Act | ||
const { rerender } = render(<Animated show={false}>Test Child</Animated>); | ||
|
||
rerender(<Animated show={true}>Test Child</Animated>); | ||
|
||
// Assert | ||
expect(screen.getByText("Test Child")).toBeInTheDocument(); | ||
|
||
// Arrange & Act | ||
rerender(<Animated show={false}>Test Child</Animated>); | ||
fireEvent.transitionEnd(screen.getByText("Test Child")); | ||
|
||
// Assert | ||
expect(screen.queryByText("Test Child")).not.toBeInTheDocument(); | ||
}); | ||
}); |
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,38 @@ | ||
import { Box } from "@saleor/macaw-ui-next"; | ||
import React, { useEffect, useState } from "react"; | ||
|
||
interface FadeProps { | ||
children: React.ReactNode; | ||
show: boolean; | ||
} | ||
|
||
export const Animated = ({ children, show }: FadeProps) => { | ||
const [shouldRender, setShouldRender] = useState(show); | ||
const [isVisible, setIsVisible] = useState(show); | ||
|
||
useEffect(() => { | ||
if (show) { | ||
setShouldRender(true); | ||
setTimeout(() => setIsVisible(true), 10); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}, [show]); | ||
|
||
const onTransitionEnd = () => { | ||
if (!show) { | ||
setShouldRender(false); | ||
} | ||
}; | ||
|
||
return shouldRender ? ( | ||
<Box | ||
__transition="opacity 0.3s ease-in-out, margin-top 0.3s ease-in-out" | ||
__opacity={isVisible ? 1 : 0} | ||
__marginTop={isVisible ? 0 : -24} | ||
onTransitionEnd={onTransitionEnd} | ||
> | ||
{children} | ||
</Box> | ||
) : null; | ||
}; |
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,10 @@ | ||
import { Box } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
|
||
interface ContainerProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const Container = ({ children }: ContainerProps) => { | ||
return <Box __gridColumn="1 / span 2">{children}</Box>; | ||
}; |
39 changes: 39 additions & 0 deletions
39
src/components/ServiceWorkerPrompt/NewVersionAvailable.tsx
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,39 @@ | ||
import { Box, Button, Text } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
export const NewVersionAvailable = ({ onUpdate }: { onUpdate: () => void }) => { | ||
return ( | ||
<Box | ||
backgroundColor="warning1" | ||
paddingX={3} | ||
height={6} | ||
display="flex" | ||
justifyContent="center" | ||
alignItems="center" | ||
> | ||
<Text size={1} fontWeight="medium"> | ||
<FormattedMessage | ||
id="sngdVO" | ||
defaultMessage="New dashboard version available." | ||
description="New dashboard version available." | ||
/> | ||
</Text> | ||
<Button | ||
onClick={onUpdate} | ||
size="small" | ||
variant="tertiary" | ||
color="accent1" | ||
textDecoration="underline" | ||
__paddingRight="3px" | ||
__paddingLeft="3px" | ||
> | ||
<FormattedMessage | ||
id="t1Qs2z" | ||
defaultMessage="Click here to reload." | ||
description="Click here to reload." | ||
/> | ||
</Button> | ||
</Box> | ||
); | ||
}; |
57 changes: 57 additions & 0 deletions
57
src/components/ServiceWorkerPrompt/ServiceWorkerPrompt.test.tsx
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,57 @@ | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import { useRegisterSW } from "virtual:pwa-register/react"; | ||
|
||
import { ServiceWorkerPrompt } from "./index"; | ||
import { useNavigatorOnline } from "./useNavigatorOnline"; | ||
|
||
jest.mock("virtual:pwa-register/react"); | ||
jest.mock("./useNavigatorOnline"); | ||
jest.mock("react-intl", () => ({ | ||
FormattedMessage: ({ defaultMessage }: { defaultMessage: string }) => <>{defaultMessage}</>, | ||
})); | ||
|
||
describe("ServiceWorkerPrompt", () => { | ||
const mockUpdateServiceWorker = jest.fn(); | ||
const mockNeedRefresh = [true]; | ||
|
||
beforeEach(() => { | ||
(useRegisterSW as jest.Mock).mockReturnValue({ | ||
needRefresh: mockNeedRefresh, | ||
updateServiceWorker: mockUpdateServiceWorker, | ||
}); | ||
(useNavigatorOnline as jest.Mock).mockReturnValue(true); | ||
}); | ||
|
||
it("renders YouAreOffline when offline", () => { | ||
// Arrange | ||
(useNavigatorOnline as jest.Mock).mockReturnValue(false); | ||
|
||
// Act | ||
render(<ServiceWorkerPrompt />); | ||
|
||
// Assert | ||
expect(screen.getByText(/You are currently offline/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders NewVersionAvailable when a new version is available", () => { | ||
// Arrange & Act | ||
render(<ServiceWorkerPrompt />); | ||
|
||
// Assert | ||
expect(screen.getByText(/New dashboard version available/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it("calls updateServiceWorker and reloads the page on update", async () => { | ||
// Arrange | ||
render(<ServiceWorkerPrompt />); | ||
|
||
const updateButton = screen.getByText(/Click here to reload/i); | ||
|
||
// Act | ||
fireEvent.click(updateButton); | ||
|
||
// Assert | ||
expect(mockUpdateServiceWorker).toHaveBeenCalled(); | ||
}); | ||
}); |
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,26 @@ | ||
import { WifiOff } from "@dashboard/icons/WifiOff"; | ||
import { Box, Text } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
export const YouAreOffline = () => { | ||
return ( | ||
<Box | ||
backgroundColor="info1" | ||
paddingX={3} | ||
height={6} | ||
display="flex" | ||
justifyContent="center" | ||
alignItems="center" | ||
> | ||
<WifiOff width={3.5} /> | ||
<Text marginLeft={2} size={1} fontWeight="medium"> | ||
<FormattedMessage | ||
id="cLnkZn" | ||
defaultMessage="You are currently offline. Some features may not be available." | ||
description="You are currently offline. Some features may not be available." | ||
/> | ||
</Text> | ||
</Box> | ||
); | ||
}; |
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,67 @@ | ||
import React from "react"; | ||
import { useRegisterSW } from "virtual:pwa-register/react"; | ||
|
||
import { Animated } from "./Animated"; | ||
import { Container } from "./Container"; | ||
import { NewVersionAvailable } from "./NewVersionAvailable"; | ||
import { useNavigatorOnline } from "./useNavigatorOnline"; | ||
import { YouAreOffline } from "./YouAreOffline"; | ||
|
||
const SW_PING_INTERVAL = 5000; | ||
|
||
const ping = async (swScriptUrl: string, registration: ServiceWorkerRegistration) => { | ||
const resp = await fetch(swScriptUrl, { | ||
cache: "no-store", | ||
headers: { | ||
cache: "no-store", | ||
"cache-control": "no-cache", | ||
}, | ||
}); | ||
|
||
if (resp?.status === 200) { | ||
await registration.update(); | ||
} | ||
}; | ||
|
||
const createPingHandler = | ||
(swScriptUrl: string, registration: ServiceWorkerRegistration) => async () => { | ||
if (registration.installing || !navigator) return; | ||
|
||
if ("connection" in navigator && !navigator.onLine) return; | ||
|
||
await ping(swScriptUrl, registration); | ||
}; | ||
|
||
export const ServiceWorkerPrompt = () => { | ||
const onRegisteredSW = async ( | ||
swScriptUrl: string, | ||
registration: ServiceWorkerRegistration | undefined, | ||
) => { | ||
if (!registration) return; | ||
|
||
setInterval(createPingHandler(swScriptUrl, registration), SW_PING_INTERVAL); | ||
}; | ||
|
||
const onUpdate = async () => { | ||
await updateServiceWorker(); | ||
window.location.reload(); | ||
}; | ||
|
||
const { | ||
needRefresh: [needRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ onRegisteredSW }); | ||
|
||
const isOnline = useNavigatorOnline(navigator); | ||
|
||
return ( | ||
<Container> | ||
<Animated show={!isOnline}> | ||
<YouAreOffline /> | ||
</Animated> | ||
<Animated show={needRefresh && isOnline}> | ||
<NewVersionAvailable onUpdate={onUpdate} /> | ||
</Animated> | ||
</Container> | ||
); | ||
}; |
21 changes: 21 additions & 0 deletions
21
src/components/ServiceWorkerPrompt/useNavigatorOnline.test.ts
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,21 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useNavigatorOnline } from "./useNavigatorOnline"; | ||
|
||
describe("useNavigatorOnline", () => { | ||
it("should return true when online", () => { | ||
const navigator = { onLine: true } as Navigator; | ||
|
||
const { result } = renderHook(() => useNavigatorOnline(navigator)); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it("should return false when offline", () => { | ||
const navigator = { onLine: false } as Navigator; | ||
|
||
const { result } = renderHook(() => useNavigatorOnline(navigator)); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temporary, testing purposes