Compare commits
70 Commits
Author | SHA1 | Date |
---|---|---|
|
97e98a0b14 | 3 years ago |
|
ddf4bd236a | 3 years ago |
|
3567a68871 | 3 years ago |
|
0c531040c5 | 3 years ago |
|
3c0181ab12 | 4 years ago |
|
419c8322b8 | 4 years ago |
|
3506b65505 | 4 years ago |
|
7836d6775d | 4 years ago |
|
89cb364080 | 4 years ago |
|
b9eae69630 | 4 years ago |
|
75f7496ea9 | 4 years ago |
|
0b8565870e | 4 years ago |
|
905448a645 | 4 years ago |
|
07d061e47f | 4 years ago |
|
c16f24956d | 4 years ago |
|
594502e21c | 4 years ago |
|
d907aa3765 | 4 years ago |
|
a65ca22604 | 4 years ago |
|
58dbcec0a3 | 4 years ago |
|
c9d36958f5 | 4 years ago |
|
ab52c8925f | 4 years ago |
|
ae8a9e9ba8 | 4 years ago |
|
575f76d0c9 | 4 years ago |
|
4c38abd443 | 4 years ago |
|
ffe45c9218 | 4 years ago |
|
ca892ece4b | 5 years ago |
|
fb570ff06f | 5 years ago |
|
96c86be205 | 5 years ago |
|
7db5041536 | 5 years ago |
|
48dfd02d30 | 5 years ago |
|
33818fa21b | 5 years ago |
|
c82202df69 | 5 years ago |
|
f1f94bafe5 | 5 years ago |
|
070dc5aebb | 5 years ago |
|
393c96d2b0 | 5 years ago |
|
d3816d30e1 | 5 years ago |
|
b15e7876ff | 5 years ago |
|
8f67f9abba | 5 years ago |
|
27ef34a7e8 | 5 years ago |
|
0f517562d0 | 5 years ago |
|
e7af4b8ebb | 5 years ago |
|
bc051eb636 | 5 years ago |
|
94322dc804 | 5 years ago |
|
17835cfcca | 5 years ago |
|
c1dda4bacc | 6 years ago |
|
0ffdfacafe | 6 years ago |
|
7c97f7b55b | 6 years ago |
|
6c40f6bee2 | 6 years ago |
|
da9c2c5ea0 | 6 years ago |
|
f1a254e926 | 6 years ago |
|
60a8055a1a | 6 years ago |
|
c53710670a | 6 years ago |
|
c074dea440 | 6 years ago |
|
ef9728b509 | 6 years ago |
|
9cf9a79f19 | 6 years ago |
|
3db04fdf29 | 6 years ago |
|
8437d717cf | 6 years ago |
|
c33ee9e1e9 | 6 years ago |
|
a664f07aa0 | 6 years ago |
|
1c5249bd4f | 6 years ago |
|
64bfab91f5 | 6 years ago |
|
38f3e88f72 | 6 years ago |
|
8a75b46f51 | 6 years ago |
|
7895003b44 | 6 years ago |
|
8a8591cb93 | 6 years ago |
|
2ceadb90f5 | 6 years ago |
|
fdeb281d8b | 6 years ago |
|
9a3660bb6d | 6 years ago |
|
59a55d37a8 | 6 years ago |
|
884d50c9c5 | 6 years ago |
@ -0,0 +1,54 @@ |
|||||||
|
name: Build & deploy |
||||||
|
|
||||||
|
on: |
||||||
|
push: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
pull_request: |
||||||
|
branches: |
||||||
|
- master |
||||||
|
|
||||||
|
jobs: |
||||||
|
build: |
||||||
|
name: Build |
||||||
|
runs-on: ubuntu-latest |
||||||
|
|
||||||
|
steps: |
||||||
|
- name: Checkout code |
||||||
|
uses: actions/checkout@v2 |
||||||
|
|
||||||
|
- name: Install Node.js |
||||||
|
uses: actions/setup-node@v1 |
||||||
|
with: |
||||||
|
node-version: 13.x |
||||||
|
|
||||||
|
- name: Install NPM packages |
||||||
|
run: npm ci |
||||||
|
|
||||||
|
- name: Build project |
||||||
|
run: npm run build |
||||||
|
|
||||||
|
- name: Upload production-ready build files |
||||||
|
uses: actions/upload-artifact@v2 |
||||||
|
with: |
||||||
|
name: production-files |
||||||
|
path: ./build |
||||||
|
|
||||||
|
deploy: |
||||||
|
name: Deploy |
||||||
|
needs: build |
||||||
|
runs-on: ubuntu-latest |
||||||
|
if: github.ref == 'refs/heads/master' |
||||||
|
|
||||||
|
steps: |
||||||
|
- name: Download artifact |
||||||
|
uses: actions/download-artifact@v2 |
||||||
|
with: |
||||||
|
name: production-files |
||||||
|
path: ./build |
||||||
|
|
||||||
|
- name: Deploy to gh-pages |
||||||
|
uses: peaceiris/actions-gh-pages@v3 |
||||||
|
with: |
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }} |
||||||
|
publish_dir: ./build |
@ -1,68 +1,14 @@ |
|||||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
## SkillDisplay |
||||||
|
A simple no-frills ACTWebSocket overlay for showing actions pressed in an overlay window. |
||||||
|
|
||||||
## Available Scripts |
## Installation |
||||||
|
Make sure the 'Using BeforeLogLineRead' box is checked in ACTWebSocket then click the 'Add URL' button to add this URL: |
||||||
|
`https://rawrington.github.io/SkillDisplay/` |
||||||
|
|
||||||
In the project directory, you can run: |
## Troubleshooting |
||||||
|
I have only tested and confirmed this working on the QT5.8.0 variant of OverlayProc with ACTWebSocket 1.3.3.9 so if you are having trouble please use these versions. |
||||||
|
|
||||||
### `npm start` |
If you find any strange bugs, please report to me on here or find me on the ACT FFXIV discord. |
||||||
|
|
||||||
Runs the app in the development mode.<br> |
## Preview |
||||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
 |
||||||
|
|
||||||
The page will reload if you make edits.<br> |
|
||||||
You will also see any lint errors in the console. |
|
||||||
|
|
||||||
### `npm test` |
|
||||||
|
|
||||||
Launches the test runner in the interactive watch mode.<br> |
|
||||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. |
|
||||||
|
|
||||||
### `npm run build` |
|
||||||
|
|
||||||
Builds the app for production to the `build` folder.<br> |
|
||||||
It correctly bundles React in production mode and optimizes the build for the best performance. |
|
||||||
|
|
||||||
The build is minified and the filenames include the hashes.<br> |
|
||||||
Your app is ready to be deployed! |
|
||||||
|
|
||||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
|
||||||
|
|
||||||
### `npm run eject` |
|
||||||
|
|
||||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** |
|
||||||
|
|
||||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. |
|
||||||
|
|
||||||
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. |
|
||||||
|
|
||||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. |
|
||||||
|
|
||||||
## Learn More |
|
||||||
|
|
||||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). |
|
||||||
|
|
||||||
To learn React, check out the [React documentation](https://reactjs.org/). |
|
||||||
|
|
||||||
### Code Splitting |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting |
|
||||||
|
|
||||||
### Analyzing the Bundle Size |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size |
|
||||||
|
|
||||||
### Making a Progressive Web App |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app |
|
||||||
|
|
||||||
### Advanced Configuration |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration |
|
||||||
|
|
||||||
### Deployment |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment |
|
||||||
|
|
||||||
### `npm run build` fails to minify |
|
||||||
|
|
||||||
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify |
|
||||||
|
After Width: | Height: | Size: 199 KiB |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,42 @@ |
|||||||
|
const getHost = () => /[?&]HOST_PORT=(wss?:\/\/[^&/]+)/.exec(window.location.search) |
||||||
|
|
||||||
|
export default function listenToACT(callback) { |
||||||
|
if (!getHost()) return listenOverlayPlugin(callback) |
||||||
|
return listenActWebSocket(callback) |
||||||
|
} |
||||||
|
|
||||||
|
function listenActWebSocket(callback) { |
||||||
|
const wsUri = `${getHost()[1]}/BeforeLogLineRead` || undefined |
||||||
|
const ws = new WebSocket(wsUri) |
||||||
|
ws.onerror = () => ws.close() |
||||||
|
ws.onclose = () => |
||||||
|
setTimeout(() => { |
||||||
|
listenActWebSocket(callback) |
||||||
|
}, 1000) |
||||||
|
ws.onmessage = function(e, m) { |
||||||
|
if (e.data === ".") return ws.send(".") |
||||||
|
|
||||||
|
const obj = JSON.parse(e.data) |
||||||
|
if (obj.msgtype === "SendCharName") { |
||||||
|
return callback(obj.msg) |
||||||
|
} else if (obj.msgtype === "Chat") { |
||||||
|
return callback(...obj.msg.split("|")) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return () => { |
||||||
|
ws.close() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function listenOverlayPlugin(callback) { |
||||||
|
const listener = e => { |
||||||
|
callback(...e.detail) |
||||||
|
} |
||||||
|
|
||||||
|
document.addEventListener("onLogLine", listener) |
||||||
|
|
||||||
|
return () => { |
||||||
|
document.removeEventListener("onLogLine", listener) |
||||||
|
} |
||||||
|
} |
@ -1,23 +0,0 @@ |
|||||||
export default function listenActWebSocket(callback) { |
|
||||||
const url = new URLSearchParams(window.location.search) |
|
||||||
const wsUri = `${url.get('HOST_PORT')}BeforeLogLineRead` || undefined |
|
||||||
const ws = new WebSocket(wsUri) |
|
||||||
ws.onerror = () => listenActWebSocket() |
|
||||||
ws.onmessage = function (e, m) { //PING
|
|
||||||
if (e.data === '.') return ws.send('.') //PONG
|
|
||||||
|
|
||||||
const obj = JSON.parse(e.data) |
|
||||||
if(obj.msgtype === 'SendCharName') |
|
||||||
{ |
|
||||||
console.log(obj.msg.charID) |
|
||||||
console.log(obj.msg.charName) |
|
||||||
return callback(obj.msg) |
|
||||||
} |
|
||||||
else if(obj.msgtype === 'Chat') |
|
||||||
{ |
|
||||||
const code = obj.msg.substring(0, 2) //first 2 numbers POG
|
|
||||||
|
|
||||||
if(code === '21' || code === '22') return callback(obj.msg) //NetworkAbility or NetworkAoeAbility
|
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,45 +1,101 @@ |
|||||||
import React from 'react' |
import React from "react" |
||||||
import './css/Action.css' |
import "./css/Action.css" |
||||||
|
|
||||||
class Action extends React.Component { |
const gcdOverrides = new Set([ |
||||||
state = { |
15997, //standard step
|
||||||
xivapi_data: [] |
15998, //technical step
|
||||||
} |
15999, |
||||||
|
16000, |
||||||
|
16001, |
||||||
|
16002, //step actions
|
||||||
|
16003, //standard finish
|
||||||
|
16004, //technical finish
|
||||||
|
16191, //single standard finish
|
||||||
|
16192, //double standard finish (WHY IS IT LIKE THIS)
|
||||||
|
16193, //single technical finish
|
||||||
|
16194, //double technical finish
|
||||||
|
16195, //triple technical finish
|
||||||
|
16196, //quadruple technical finish
|
||||||
|
7418, //flamethrower
|
||||||
|
16484, //kaeshi higanbana
|
||||||
|
16485, //kaeshi goken
|
||||||
|
16486, //kaeshi setsugekka
|
||||||
|
2259, //ten
|
||||||
|
18805,
|
||||||
|
2261, //chi
|
||||||
|
18806, |
||||||
|
2263, //jin
|
||||||
|
18807, |
||||||
|
2265, //fuma shurikan
|
||||||
|
18873, |
||||||
|
18874, |
||||||
|
18875, |
||||||
|
2267, //raiton
|
||||||
|
18877, |
||||||
|
2266, //katon
|
||||||
|
18876, |
||||||
|
2268, //hyoton
|
||||||
|
18878, |
||||||
|
16492, //hyosho ranryu
|
||||||
|
16471, //goka meykakku
|
||||||
|
16491, //goka meykakku (16471 is the PvP version, 16491 is the PvE version)
|
||||||
|
2270, //doton
|
||||||
|
18880, |
||||||
|
2269, //huton
|
||||||
|
18879, |
||||||
|
2271, //suiton
|
||||||
|
18881, |
||||||
|
2272, //rabbit medium
|
||||||
|
]) |
||||||
|
|
||||||
constructor(props) { |
const ogcdOverrides = new Set([ |
||||||
super(props); |
3559, //bard WM
|
||||||
|
116, //bard AP
|
||||||
|
114 //bard MB
|
||||||
|
]) |
||||||
|
|
||||||
const actionUrl = "https://xivapi.com/Action/"+props.action_id; |
const actionMap = new Map() |
||||||
|
|
||||||
console.log(actionUrl) |
export default function Action({ actionId, additionalClasses }) { |
||||||
|
const [apiData, setApiData] = React.useState() |
||||||
|
|
||||||
fetch(actionUrl, { mode: 'cors' }) |
React.useEffect(() => { |
||||||
.then(response => response.json()) |
const mapData = actionMap.get(actionId) |
||||||
.then(data => {this.setState({xivapi_data: data})}) |
if (mapData != null) { |
||||||
|
setApiData(mapData) |
||||||
|
return |
||||||
} |
} |
||||||
|
|
||||||
shouldComponentUpdate() { |
let current = true |
||||||
if (Object.getOwnPropertyNames(this.state.xivapi_data).length === 0) { |
void (async () => { |
||||||
return false |
const data = await (await fetch(`https://xivapi.com/Action/${actionId}?columns=Icon,Name,ActionCategoryTargetID`, { |
||||||
} |
mode: "cors" |
||||||
return true |
})).json() |
||||||
|
if (current) { |
||||||
|
actionMap.set(actionId, data) |
||||||
|
setApiData(data) |
||||||
} |
} |
||||||
|
})() |
||||||
|
|
||||||
isGCD() { |
return () => { |
||||||
return (this.state.xivapi_data.ActionCategory.ID !== 4) |
current = false |
||||||
} |
} |
||||||
|
}, [actionId]) |
||||||
|
|
||||||
render() { |
if (apiData === undefined || !apiData.Icon) { |
||||||
if(this.state.xivapi_data.Icon) { |
|
||||||
const classes = this.isGCD()?'action-icon gcd':'action-icon ogcd' |
|
||||||
const img = "https://xivapi.com"+this.state.xivapi_data.Icon |
|
||||||
return <img className={classes} src={img} alt='' /> |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
return null |
return null |
||||||
} |
} |
||||||
|
|
||||||
|
return ( |
||||||
|
<img |
||||||
|
className={ |
||||||
|
gcdOverrides.has(actionId) || |
||||||
|
(!ogcdOverrides.has(actionId) && apiData.ActionCategoryTargetID !== 4) |
||||||
|
? `gcd ${additionalClasses}` |
||||||
|
: `ogcd ${additionalClasses}` |
||||||
} |
} |
||||||
|
src={`https://xivapi.com/${apiData.Icon}`} |
||||||
|
alt={apiData.Name || ""} |
||||||
|
/> |
||||||
|
) |
||||||
} |
} |
||||||
|
|
||||||
export default Action |
|
@ -0,0 +1,33 @@ |
|||||||
|
import React from "react" |
||||||
|
import "./css/Rotation.css" |
||||||
|
import Action from "./Action" |
||||||
|
|
||||||
|
export default function RotationContainer({ encounterId, name, actionList }) { |
||||||
|
const [open, setOpen] = React.useState(false) |
||||||
|
|
||||||
|
return ( |
||||||
|
<> |
||||||
|
<button |
||||||
|
className={open ? "rotation-button expanded" : "rotation-button"} |
||||||
|
onClick={() => { |
||||||
|
setOpen(open => !open) |
||||||
|
}} |
||||||
|
> |
||||||
|
{encounterId === 0 ? "Current Rotation" : name} |
||||||
|
</button> |
||||||
|
<RotationContents expanded={open} actionList={actionList} /> |
||||||
|
</> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
function RotationContents({ expanded, actionList }) { |
||||||
|
if (!expanded) return null |
||||||
|
|
||||||
|
return ( |
||||||
|
<div className="rotation-list"> |
||||||
|
{actionList.map((action, i) => ( |
||||||
|
<Action key={i} actionId={action} additionalClasses="action-rotation" /> |
||||||
|
))} |
||||||
|
</div> |
||||||
|
) |
||||||
|
} |
@ -1,17 +1,40 @@ |
|||||||
|
body { |
||||||
|
margin: 0; |
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", |
||||||
|
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", |
||||||
|
sans-serif; |
||||||
|
-webkit-font-smoothing: antialiased; |
||||||
|
-moz-osx-font-smoothing: grayscale; |
||||||
|
font-size: 16px; |
||||||
|
} |
||||||
|
|
||||||
|
html { |
||||||
|
height: 100vh; |
||||||
|
overflow: hidden; |
||||||
|
background-image: none; |
||||||
|
background-repeat: no-repeat; |
||||||
|
} |
||||||
|
|
||||||
.actions { |
.actions { |
||||||
margin-top: 1em; |
padding-top: 1rem; |
||||||
background: linear-gradient(180deg, |
background: linear-gradient(180deg, |
||||||
|
rgba(0,0,0,0) calc(25% - 1px), |
||||||
|
rgba(255,255,255,0.5) calc(25%), |
||||||
|
rgba(0,0,0,0) calc(25% + 1px), |
||||||
rgba(0,0,0,0) calc(50% - 1px), |
rgba(0,0,0,0) calc(50% - 1px), |
||||||
rgba(255,255,255,1) calc(50%), |
rgba(255,255,255,0.5) calc(50%), |
||||||
rgba(0,0,0,0) calc(50% + 1px) |
rgba(0,0,0,0) calc(50% + 1px), |
||||||
|
rgba(0,0,0,0) calc(75% - 1px), |
||||||
|
rgba(255,255,255,0.5) calc(75%), |
||||||
|
rgba(0,0,0,0) calc(75% + 1px) |
||||||
); |
); |
||||||
height: 3em; |
height: 3rem; |
||||||
|
padding-bottom: 1rem; |
||||||
position: absolute; |
background-color: rgba(20, 20, 20, 0.3); |
||||||
top:0; |
display: inline-block; |
||||||
bottom: 0; |
} |
||||||
left: 0; |
|
||||||
right: 0; |
|
||||||
|
|
||||||
margin: auto; |
.container { |
||||||
|
display: flex; |
||||||
|
flex-direction: column; |
||||||
} |
} |
@ -0,0 +1,32 @@ |
|||||||
|
.rotation-list { |
||||||
|
padding: 0 18px; |
||||||
|
background-color: rgba(80, 80, 80, 0.3); |
||||||
|
display: block; |
||||||
|
} |
||||||
|
|
||||||
|
.rotation-button { |
||||||
|
background-color: rgba(100, 100, 100, 0.3); |
||||||
|
color: rgba(255, 255, 255, 0.5); |
||||||
|
padding: 0.1em; |
||||||
|
width: 100vw; |
||||||
|
border: none; |
||||||
|
outline: none; |
||||||
|
transition: 0.4s; |
||||||
|
display: block; |
||||||
|
} |
||||||
|
|
||||||
|
.rotation-button.expanded, .rotation-button:hover { |
||||||
|
background-color: rgba(120, 120, 120, 0.3); |
||||||
|
} |
||||||
|
|
||||||
|
.rotation-button:after { |
||||||
|
content: '\25BC'; |
||||||
|
font-size: 13px; |
||||||
|
color: rgb(119,119,119); |
||||||
|
float: right; |
||||||
|
margin-left: 5px; |
||||||
|
} |
||||||
|
|
||||||
|
.rotation-button.expanded:after { |
||||||
|
content: "\25B2"; |
||||||
|
} |
@ -1,12 +1,5 @@ |
|||||||
import React from 'react'; |
import React from 'react' |
||||||
import ReactDOM from 'react-dom'; |
import ReactDOM from 'react-dom' |
||||||
import './css/index.css'; |
import App from './App' |
||||||
import App from './App'; |
|
||||||
import * as serviceWorker from './serviceWorker'; |
|
||||||
|
|
||||||
ReactDOM.render(<App />, document.getElementById('root')); |
ReactDOM.render(<App />, document.getElementById('root')) |
||||||
|
|
||||||
// If you want your app to work offline and load faster, you can change
|
|
||||||
// unregister() to register() below. Note this comes with some pitfalls.
|
|
||||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
|
||||||
serviceWorker.unregister(); |
|
||||||
|
@ -1,135 +0,0 @@ |
|||||||
// This optional code is used to register a service worker.
|
|
||||||
// register() is not called by default.
|
|
||||||
|
|
||||||
// This lets the app load faster on subsequent visits in production, and gives
|
|
||||||
// it offline capabilities. However, it also means that developers (and users)
|
|
||||||
// will only see deployed updates on subsequent visits to a page, after all the
|
|
||||||
// existing tabs open on the page have been closed, since previously cached
|
|
||||||
// resources are updated in the background.
|
|
||||||
|
|
||||||
// To learn more about the benefits of this model and instructions on how to
|
|
||||||
// opt-in, read https://bit.ly/CRA-PWA
|
|
||||||
|
|
||||||
const isLocalhost = Boolean( |
|
||||||
window.location.hostname === 'localhost' || |
|
||||||
// [::1] is the IPv6 localhost address.
|
|
||||||
window.location.hostname === '[::1]' || |
|
||||||
// 127.0.0.1/8 is considered localhost for IPv4.
|
|
||||||
window.location.hostname.match( |
|
||||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ |
|
||||||
) |
|
||||||
); |
|
||||||
|
|
||||||
export function register(config) { |
|
||||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { |
|
||||||
// The URL constructor is available in all browsers that support SW.
|
|
||||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); |
|
||||||
if (publicUrl.origin !== window.location.origin) { |
|
||||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
|
||||||
// from what our page is served on. This might happen if a CDN is used to
|
|
||||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
window.addEventListener('load', () => { |
|
||||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; |
|
||||||
|
|
||||||
if (isLocalhost) { |
|
||||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
|
||||||
checkValidServiceWorker(swUrl, config); |
|
||||||
|
|
||||||
// Add some additional logging to localhost, pointing developers to the
|
|
||||||
// service worker/PWA documentation.
|
|
||||||
navigator.serviceWorker.ready.then(() => { |
|
||||||
console.log( |
|
||||||
'This web app is being served cache-first by a service ' + |
|
||||||
'worker. To learn more, visit https://bit.ly/CRA-PWA' |
|
||||||
); |
|
||||||
}); |
|
||||||
} else { |
|
||||||
// Is not localhost. Just register service worker
|
|
||||||
registerValidSW(swUrl, config); |
|
||||||
} |
|
||||||
}); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
function registerValidSW(swUrl, config) { |
|
||||||
navigator.serviceWorker |
|
||||||
.register(swUrl) |
|
||||||
.then(registration => { |
|
||||||
registration.onupdatefound = () => { |
|
||||||
const installingWorker = registration.installing; |
|
||||||
if (installingWorker == null) { |
|
||||||
return; |
|
||||||
} |
|
||||||
installingWorker.onstatechange = () => { |
|
||||||
if (installingWorker.state === 'installed') { |
|
||||||
if (navigator.serviceWorker.controller) { |
|
||||||
// At this point, the updated precached content has been fetched,
|
|
||||||
// but the previous service worker will still serve the older
|
|
||||||
// content until all client tabs are closed.
|
|
||||||
console.log( |
|
||||||
'New content is available and will be used when all ' + |
|
||||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.' |
|
||||||
); |
|
||||||
|
|
||||||
// Execute callback
|
|
||||||
if (config && config.onUpdate) { |
|
||||||
config.onUpdate(registration); |
|
||||||
} |
|
||||||
} else { |
|
||||||
// At this point, everything has been precached.
|
|
||||||
// It's the perfect time to display a
|
|
||||||
// "Content is cached for offline use." message.
|
|
||||||
console.log('Content is cached for offline use.'); |
|
||||||
|
|
||||||
// Execute callback
|
|
||||||
if (config && config.onSuccess) { |
|
||||||
config.onSuccess(registration); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
}; |
|
||||||
}; |
|
||||||
}) |
|
||||||
.catch(error => { |
|
||||||
console.error('Error during service worker registration:', error); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
function checkValidServiceWorker(swUrl, config) { |
|
||||||
// Check if the service worker can be found. If it can't reload the page.
|
|
||||||
fetch(swUrl) |
|
||||||
.then(response => { |
|
||||||
// Ensure service worker exists, and that we really are getting a JS file.
|
|
||||||
const contentType = response.headers.get('content-type'); |
|
||||||
if ( |
|
||||||
response.status === 404 || |
|
||||||
(contentType != null && contentType.indexOf('javascript') === -1) |
|
||||||
) { |
|
||||||
// No service worker found. Probably a different app. Reload the page.
|
|
||||||
navigator.serviceWorker.ready.then(registration => { |
|
||||||
registration.unregister().then(() => { |
|
||||||
window.location.reload(); |
|
||||||
}); |
|
||||||
}); |
|
||||||
} else { |
|
||||||
// Service worker found. Proceed as normal.
|
|
||||||
registerValidSW(swUrl, config); |
|
||||||
} |
|
||||||
}) |
|
||||||
.catch(() => { |
|
||||||
console.log( |
|
||||||
'No internet connection found. App is running in offline mode.' |
|
||||||
); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
export function unregister() { |
|
||||||
if ('serviceWorker' in navigator) { |
|
||||||
navigator.serviceWorker.ready.then(registration => { |
|
||||||
registration.unregister(); |
|
||||||
}); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue