GoogleChromeLabs / sw-precache

[Deprecated] A node module to generate service worker code that will precache specific resources so they work offline.
https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw
Apache License 2.0
5.22k stars 388 forks source link

`navigateFallback` with express routing (return 404) #294

Closed Shyam-Chen closed 7 years ago

Shyam-Chen commented 7 years ago

https://github.com/GoogleChrome/sw-precache/blob/master/app-shell-demo/gulpfile.babel.js#L140

[...]
  navigateFallback: 'index.html',
[...]

http://localhost:8000/list <- 404

[...]
  navigateFallbackWhitelist: [/^\/list\//],
[...]
import { Router } from 'express';

import { List } from '../models';

const router = Router();

router.get('/', (req, res, next) => {
  List.find({}, (err, data) => {
    if (err) return next(err);
    res.json(data);
  });
});

router.get('/:text', (req, res, next) => {
  const list = new List({
    text: req.params.text,
    created: new Date()
  });

  list.save(err => {
    if (err) return next(err);
    console.log('List saved successfully.');
    res.redirect('/list');
  });
});

export const listRoutes = router;
[...]
app.use('/list', listRoutes);

app.use(express.static(root));
[...]
coryshaw commented 5 years ago

@Shyam-Chen did you figure out the solution? Having the same issue.

Shyam-Chen commented 5 years ago

Change to Workbox.

// ...
const { GenerateSW } = require('workbox-webpack-plugin');

// ...
    new GenerateSW({
      skipWaiting: true,
      clientsClaim: true,
      runtimeCaching: [{
        urlPattern: new RegExp(env.SITE_URL),
        handler: 'staleWhileRevalidate',
      }],
      navigateFallback: '/',
      navigateFallbackWhitelist: [/^(?!\/__).*/],
      cacheId: pkg.name,
    }),
// ...