-
Notifications
You must be signed in to change notification settings - Fork 3
добавлен Service Worker #36
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
Open
bulgakovd97
wants to merge
5
commits into
develop
Choose a base branch
from
43_service-workers
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
36 changes: 19 additions & 17 deletions
36
packages/client/src/fuature/forum/components/comments/components/footer/Footer.tsx
This file contains hidden or 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,22 +1,24 @@ | ||
| import React, { useState } from 'react' | ||
| import styles from './Footer.module.scss' | ||
| import { Title } from '@components/design-system/Fonts' | ||
| import { Button } from '@components/design-system' | ||
| import React, { useState } from 'react'; | ||
| import styles from './Footer.module.scss'; | ||
| import { Title } from '@components/design-system/Fonts'; | ||
| import { Button } from '@components/design-system'; | ||
|
|
||
| export const Footer: React.FC = () => { | ||
| const [comment, setComment] = useState<string>(''); | ||
|
|
||
| export const Footer:React.FC = () => { | ||
| const { comment, setComment } = useState(''); | ||
| return ( | ||
| <div className={ styles.comment__footer }> | ||
| <Title className={ styles.comment_add__title }>Оставить комментарий</Title> | ||
| <form className={ styles.comment_add__form }> | ||
| <textarea className={ styles.comment__textaria } | ||
| name='comment' | ||
| required value={ comment } | ||
| onChange={ (e) => setComment(e.target.value) } | ||
| > | ||
| </textarea> | ||
| <Button className={ styles.comment_add__btn }>Добавить</Button> | ||
| <div className={styles.comment__footer}> | ||
| <Title className={styles.comment_add__title}>Оставить комментарий</Title> | ||
| <form className={styles.comment_add__form}> | ||
| <textarea | ||
| className={styles.comment__textaria} | ||
| name="comment" | ||
| required | ||
| value={comment} | ||
| onChange={(e) => setComment(e.target.value)} | ||
| ></textarea> | ||
| <Button className={styles.comment_add__btn}>Добавить</Button> | ||
| </form> | ||
| </div> | ||
| ); | ||
| } | ||
| }; |
35 changes: 20 additions & 15 deletions
35
packages/client/src/fuature/forum/components/fullscreen/FullScreen.tsx
This file contains hidden or 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,30 +1,35 @@ | ||
| import React, { useState } from 'react' | ||
| import styles from './FullScreen.module.scss' | ||
| import classNames from 'classnames' | ||
| import React, { useState } from 'react'; | ||
| import clsx from 'clsx'; | ||
| import styles from './FullScreen.module.scss'; | ||
|
|
||
| export const FullScreen: React.FC = () => { | ||
| const [isFullscreeen, setFullscreen] = useState(false) | ||
| const [isFullscreeen, setFullscreen] = useState(false); | ||
|
|
||
| async function eventHandler(event: React.MouseEvent<HTMLDivElement | HTMLButtonElement>) { | ||
| if (document.fullscreenElement) { | ||
| try { | ||
| await document.exitFullscreen() | ||
| setFullscreen(false) | ||
| await document.exitFullscreen(); | ||
| setFullscreen(false); | ||
| } catch (err) { | ||
| throw new Error() | ||
| throw new Error(); | ||
| } | ||
| } else { | ||
| try { | ||
| await document.documentElement.requestFullscreen() | ||
| setFullscreen(true) | ||
| await document.documentElement.requestFullscreen(); | ||
| setFullscreen(true); | ||
| } catch (err) { | ||
| throw new Error() | ||
| throw new Error(); | ||
| } | ||
| } | ||
| } | ||
| return ( | ||
| <button className={classNames(styles.iconFullScreen, { | ||
| [styles.fullScreenActive]: isFullscreeen | ||
| })} type='submit' id='toggler' onClick={(e) => eventHandler(e)}></button> | ||
| ) | ||
| } | ||
| <button | ||
| className={clsx(styles.iconFullScreen, { | ||
| [styles.fullScreenActive]: isFullscreeen, | ||
| })} | ||
| type="submit" | ||
| id="toggler" | ||
| onClick={(e) => eventHandler(e)} | ||
| ></button> | ||
| ); | ||
| }; |
10 changes: 5 additions & 5 deletions
10
packages/client/src/fuature/forum/components/topics/TopicElements/TopicElements.tsx
This file contains hidden or 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,11 +1,11 @@ | ||
| import React from 'react'; | ||
| import { TopicsItem } from '../TopicItem' | ||
| import { TopicsItem } from '../TopicItem'; | ||
|
|
||
| export const TopicElements = () => { | ||
| const topicsData = [ | ||
| { id:1, title: 'Какие игры сейчас популярны', desc: 'Dota, CS, CS:GO', timestamp: '10 янв 2023' }, | ||
| { id:2, title: 'Го играть', desc: 'Я бы сыграл в денди', timestamp: '30 июня 2023' }, | ||
| { id:3, title: 'Я доделал форум)', desc: 'Почти в дед лайн =)', timestamp: '01 июля 2023' }, | ||
| { id: '1', title: 'Какие игры сейчас популярны', desc: 'Dota, CS, CS:GO', timestamp: '10 янв 2023' }, | ||
| { id: '2', title: 'Го играть', desc: 'Я бы сыграл в денди', timestamp: '30 июня 2023' }, | ||
| { id: '3', title: 'Я доделал форум)', desc: 'Почти в дед лайн =)', timestamp: '01 июля 2023' }, | ||
| ]; | ||
| return topicsData.map( element => <TopicsItem topics ={ element }/>) | ||
| return topicsData.map((element) => <TopicsItem topics={element} />); | ||
| }; |
This file contains hidden or 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 hidden or 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,15 @@ | ||
| export function startServiceWorker() { | ||
| if ('serviceWorker' in navigator) { | ||
| window.addEventListener('load', async () => { | ||
| const sw = '/src/utils/sw/sw.ts'; | ||
|
|
||
| try { | ||
| const registration = await navigator.serviceWorker.register(sw); | ||
|
|
||
| console.log('ServiceWorker registration successful with scope: ', registration.scope); | ||
| } catch (error) { | ||
| console.log('ServiceWorker registration failed: ', error); | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,108 @@ | ||
| /// <reference lib="webworker" /> | ||
|
|
||
| const sw = self as unknown as ServiceWorkerGlobalScope & typeof globalThis; | ||
|
|
||
| const staticCacheName = 'static-cache-v1'; | ||
| const dynamicCacheName = 'dynamic-cache-v1'; | ||
|
|
||
| const assetUrls = [ | ||
| '/', | ||
| '/index.html', | ||
| '/src/index.tsx', | ||
| '/src/styles/index.scss', | ||
| '/src/styles/images/casper.png', | ||
| '/src/styles/images/add_msg.png', | ||
| '/src/styles/images/auth.png', | ||
| '/src/styles/images/error.png', | ||
| '/src/styles/images/forum-left-side.svg', | ||
| '/src/styles/images/forum.svg', | ||
| '/src/styles/images/fullscreen-close.png', | ||
| '/src/styles/images/fullscreen-open.png', | ||
| '/src/styles/images/fullscreen.png', | ||
| '/src/styles/images/Hipe.svg', | ||
| '/src/styles/images/leader.svg', | ||
| '/src/styles/images/leaders.svg', | ||
| '/src/styles/images/leaderVector.png', | ||
| '/src/styles/images/like.png', | ||
| '/src/styles/images/profile.svg', | ||
| '/src/styles/images/progress-bg-pic.svg', | ||
| '/src/styles/images/progress-pic.svg', | ||
| '/src/styles/images/progress.svg', | ||
| '/src/styles/images/start.svg', | ||
| '/src/styles/fonts/HeliosExt-Bold.ttf', | ||
| '/src/styles/fonts/HeliosExt.ttf', | ||
| '/src/styles/fonts/Roboto-Bold.eot', | ||
| '/src/styles/fonts/Roboto-Bold.ttf', | ||
| '/src/styles/fonts/Roboto-Bold.woff', | ||
| '/src/styles/fonts/Roboto-Bold.woff2', | ||
| '/src/styles/fonts/Roboto-Regular.eot', | ||
| '/src/styles/fonts/Roboto-Regular.ttf', | ||
| '/src/styles/fonts/Roboto-Regular.woff', | ||
| '/src/styles/fonts/Roboto-Regular.woff2', | ||
| ]; | ||
|
|
||
| sw.addEventListener('install', (event) => { | ||
| event.waitUntil(addResourcesToCache(assetUrls)); | ||
| }); | ||
|
|
||
| sw.addEventListener('activate', async (event) => { | ||
| try { | ||
| const cacheNames = await caches.keys(); | ||
|
|
||
| await Promise.all( | ||
| cacheNames | ||
| .filter((name) => name !== staticCacheName) | ||
| .filter((name) => name !== dynamicCacheName) | ||
| .map((name) => caches.delete(name)) | ||
| ); | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| }); | ||
|
|
||
| sw.addEventListener('fetch', (event) => { | ||
| const { request } = event; | ||
|
|
||
| const url = new URL(request.url); | ||
|
|
||
| if (url.origin === location.origin) { | ||
| event.respondWith(cacheFirst(request)); | ||
| return; | ||
| } | ||
|
|
||
| // @ts-expect-error - response might be undefined | ||
| event.respondWith(networkFirst(request)); | ||
| }); | ||
|
|
||
| async function addResourcesToCache(resources: string[]) { | ||
| try { | ||
| const cache = await caches.open(staticCacheName); | ||
| await cache.addAll(resources); | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| } | ||
|
|
||
| async function cacheFirst(request: Request) { | ||
| const cached = await caches.match(request); | ||
|
|
||
| return cached ?? (await fetch(request)); | ||
| } | ||
|
|
||
| async function networkFirst(request: Request) { | ||
| const cache = await caches.open(dynamicCacheName); | ||
|
|
||
| try { | ||
| const response = await fetch(request); | ||
|
|
||
| await cache.put(request, response.clone()); | ||
|
|
||
| return response; | ||
| } catch (error) { | ||
| console.log(error); | ||
|
|
||
| const cached = await caches.match(request); | ||
|
|
||
| return cached; | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
Рекомендация: при использовании if стараться оставлять структуру без ветвлений