Skip to content

Commit

Permalink
switch back to react-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcodes committed Jun 16, 2019
1 parent fb8fb94 commit 2430f4a
Show file tree
Hide file tree
Showing 9 changed files with 5,523 additions and 3,042 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Expand Up @@ -11,10 +11,7 @@

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.env

npm-debug.log*
yarn-debug.log*
Expand Down
17 changes: 0 additions & 17 deletions build.js

This file was deleted.

6 changes: 3 additions & 3 deletions captain-definition
Expand Up @@ -8,9 +8,9 @@
"RUN npm install serve -g",
"RUN npm install",
"ENV NODE_ENV production",
"ENV SERVER https://together-server.tpxl.io",
"ENV SUBSCRIPTION_SERVER wss://together-server.tpxl.io/graphql",
"ENV REACT_APP_SERVER https://together-server.tpxl.io",
"ENV REACT_APP_SUBSCRIPTION_SERVER wss://together-server.tpxl.io/graphql",
"RUN npm run build",
"CMD ['serve', '-p', '80', '-s', './build']"
"CMD [\"serve\", \"-p\", \"80\", \"-s\", \"build\"]"
]
}
2 changes: 0 additions & 2 deletions development.env

This file was deleted.

8,357 changes: 5,383 additions & 2,974 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions package.json
Expand Up @@ -4,9 +4,7 @@
"private": true,
"devDependencies": {
"npm-run-all": "^4.1.3",
"react-app-env": "^1.2.3",
"react-scripts": "2.1.1",
"sw-precache": "^5.2.1"
"react-scripts": "^2.1.1"
},
"dependencies": {
"@material-ui/core": "^3.9.3",
Expand Down Expand Up @@ -48,11 +46,10 @@
"viewport-mercator-project": "^6.0.0"
},
"scripts": {
"start": "react-app-env start",
"build": "react-app-env build && npm run serviceWorker",
"test": "react-app-env test --env=jsdom",
"serviceWorker": "node ./build",
"eject": "react-app-env eject"
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"prettier": {
"singleQuote": true,
Expand Down
7 changes: 2 additions & 5 deletions src/index.js
Expand Up @@ -5,7 +5,7 @@ import { ApolloProvider as ApolloHooksProvider } from 'react-apollo-hooks'
import client from './modules/apollo'
import App from './containers/App'
import Theme from './components/Theme'
import serviceWorker from './service-worker'
import * as serviceWorker from './service-worker'

ReactDOM.render(
<ApolloProvider client={client}>
Expand All @@ -18,7 +18,4 @@ ReactDOM.render(
document.getElementById('root')
)

serviceWorker(() => {
console.log('Worker updated')
// store.dispatch(addNotification('App updated. Reopen Together to load update'))
})
serviceWorker.register()
5 changes: 3 additions & 2 deletions src/modules/apollo.js
Expand Up @@ -11,7 +11,7 @@ import { GET_CHANNELS } from '../queries'

// Create an http link:
const httpLink = new HttpLink({
uri: process.env.SERVER || 'http://localhost:4000',
uri: process.env.REACT_APP_SERVER || 'http://localhost:4000',
includeExtensions: true,
headers: {
authorization: localStorage.getItem('token'),
Expand All @@ -22,7 +22,8 @@ const httpLink = new HttpLink({

// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: process.env.SUBSCRIPTION_SERVER || 'ws://localhost:4000/graphql',
uri:
process.env.REACT_APP_SUBSCRIPTION_SERVER || 'ws://localhost:4000/graphql',
options: {
reconnect: true,
timeout: 30000,
Expand Down
153 changes: 126 additions & 27 deletions src/service-worker.js
@@ -1,36 +1,135 @@
export default function(cb = () => {}) {
// 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 = `/service-worker.js`
navigator.serviceWorker
.register(swUrl)
.then(reg => {
reg.onupdatefound = () => {
var installingWorker = reg.installing
installingWorker.onstatechange = () => {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
cb()
} else {
console.log('Content is now available offline!')
}
break
case 'redundant':
console.error(
'The installing service worker became redundant.'
)
break
default:
// Noting to do
break
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()
})
})
.catch(e => {
console.error('Error during service worker registration:', e)
})
} 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()
})
}
}

0 comments on commit 2430f4a

Please sign in to comment.