我无法注册Service Worker。该文件与index.js和app.js放置在同一目录下。我遇到了以下错误
A bad HTTP response code (404) was received when fetching the script.
Failed to load resource: net::ERR_INVALID_RESPONSE
Registration failed TypeError: Failed to register a ServiceWorker: A
bad HTTP response code (404) was received when fetching the script.
******
if ('serviceWorker' in navigator) {
window.addEventListener('`load`', () => {
navigator.serviceWorker.register('serviceWorker.js', { scope:'./' })
.then((registration) => {
console.log('Registration completed successfully',registration);
})
.catch((error) => {
console.log('Registration failed', error);
})
})}
index.js
和app.js
并列,而且必须位于应用程序的根目录中。因此,如果您的index.js
或app.js
在名为js
的文件夹中,它将无法正常工作。只需将serviceWorker.js
放在与index.html
相同的目录中,并请在服务器上运行它。 - andylamax