Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Meta
create-react-app
Commits
b98ba3d7
Commit
b98ba3d7
authored
7 years ago
by
Dan Abramov
Browse files
Options
Download
Email Patches
Plain Diff
Better document PWA stuff
parent
6c8b18b0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/react-scripts/template/src/index.js
+1
-2
packages/react-scripts/template/src/index.js
packages/react-scripts/template/src/registerServiceWorker.js
+51
-0
packages/react-scripts/template/src/registerServiceWorker.js
packages/react-scripts/template/src/service-worker-registration.js
+0
-38
...react-scripts/template/src/service-worker-registration.js
with
52 additions
and
40 deletions
+52
-40
packages/react-scripts/template/src/index.js
+
1
-
2
View file @
b98ba3d7
import
React
from
'
react
'
;
import
ReactDOM
from
'
react-dom
'
;
import
App
from
'
./App
'
;
import
registerServiceWorker
from
'
./
s
ervice
-w
orker
-registration
'
;
import
registerServiceWorker
from
'
./
registerS
ervice
W
orker
'
;
import
'
./index.css
'
;
ReactDOM
.
render
(
<
App
/>
,
document
.
getElementById
(
'
root
'
));
registerServiceWorker
();
This diff is collapsed.
Click to expand it.
packages/react-scripts/template/src/registerServiceWorker.js
0 → 100644
+
51
-
0
View file @
b98ba3d7
// In production, we register a service worker to serve assets from local cache.
// 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 the "N+1" visit to a page, since previously
// cached resources are updated in the background.
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
// This link also includes instructions on opting out of this behavior.
export
default
function
register
()
{
if
(
process
.
env
.
NODE_ENV
===
'
production
'
&&
'
serviceWorker
'
in
navigator
)
{
window
.
addEventListener
(
'
load
'
,
()
=>
{
const
swUrl
=
`
${
process
.
env
.
PUBLIC_URL
}
/service-worker.js`
;
navigator
.
serviceWorker
.
register
(
swUrl
)
.
then
(
registration
=>
{
registration
.
onupdatefound
=
()
=>
{
const
installingWorker
=
registration
.
installing
;
installingWorker
.
onstatechange
=
()
=>
{
if
(
installingWorker
.
state
===
'
installed
'
)
{
if
(
navigator
.
serviceWorker
.
controller
)
{
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in your web app.
console
.
log
(
'
New content is available; please refresh.
'
);
}
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.
'
);
}
}
};
};
})
.
catch
(
error
=>
{
console
.
error
(
'
Error during service worker registration:
'
,
error
);
});
});
}
}
export
function
unregister
()
{
if
(
'
serviceWorker
'
in
navigator
)
{
navigator
.
serviceWorker
.
ready
.
then
(
registration
=>
{
registration
.
unregister
();
});
}
}
This diff is collapsed.
Click to expand it.
packages/react-scripts/template/src/service-worker-registration.js
deleted
100644 → 0
+
0
-
38
View file @
6c8b18b0
export
default
function
register
()
{
if
(
process
.
env
.
NODE_ENV
===
'
production
'
&&
'
serviceWorker
'
in
navigator
)
{
window
.
addEventListener
(
'
load
'
,
()
=>
{
const
swUrl
=
`
${
process
.
env
.
PUBLIC_URL
}
/service-worker.js`
;
navigator
.
serviceWorker
.
register
(
swUrl
).
then
(
registration
=>
{
registration
.
onupdatefound
=
()
=>
{
const
installingWorker
=
registration
.
installing
;
installingWorker
.
onstatechange
=
()
=>
{
if
(
installingWorker
.
state
===
'
installed
'
)
{
if
(
navigator
.
serviceWorker
.
controller
)
{
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a "New content is
// available; please refresh." message in your web app.
console
.
log
(
'
New content is available; please refresh.
'
);
}
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.
'
);
}
}
};
};
}).
catch
(
error
=>
{
console
.
error
(
'
Error during service worker registration:
'
,
error
);
});
});
}
}
export
function
unregister
()
{
if
(
'
serviceWorker
'
in
navigator
)
{
navigator
.
serviceWorker
.
ready
.
then
(
registration
=>
{
registration
.
unregister
();
});
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment