tsepen
@tsepen
Frontend developer

Как настроить PWA и Nextjs?

Next.js, сервер на Express, как подключить next-offline?

Server.js

/* eslint-disable no-console */
const express = require('express');
const next = require('next');
const nextI18NextMiddleware = require('next-i18next/middleware');

const nextI18next = require('./app/i18n');

const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handle = app.getRequestHandler();

(async () => {
  await app.prepare();
  const server = express();

  server.use(nextI18NextMiddleware(nextI18next));

  server.get('*', (req, res) => handle(req, res));

  const port = process.env.PORT || 3000;

  await server.listen(port);
  console.log(`> Ready on PORT:${port}`);
})();


Next.config.js

require('dotenv').config();

const path = require('path');
const Dotenv = require('dotenv-webpack');
const withOffline = require('next-offline');

module.exports = withOffline({
  generateSw: false,
  workboxOpts: {
    swDest: './service-worker.js',
    swSrc: path.join(__dirname, './service-worker/index.js'),
    globPatterns: ['static/**/*'],
    globDirectory: '.'
  },

  webpack(config, options) {
    const conf = config;

    conf.plugins = conf.plugins || [];

    conf.plugins = [
      ...conf.plugins,

      new Dotenv({
        path: path.join(__dirname, '.env'),
        systemvars: true
      })
    ];

    return conf;
  }
});
  • Вопрос задан
  • 713 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы