aws-amplify / amplify-hosting

AWS Amplify Hosting provides a Git-based workflow for deploying and hosting fullstack serverless web applications.
https://aws.amazon.com/amplify/hosting/
Apache License 2.0
461 stars 116 forks source link

Nextjs app doesn't register service worker using next-pwa #2110

Open mhamendes opened 3 years ago

mhamendes commented 3 years ago

Before opening, please confirm:

App Id

dt0016ajs5y61

Region

us-east-1

Amplify Console feature

Not applicable

Describe the bug

I'm using next-pwa lib to register the service worker on my web-app.

When I deploy the same code when deployed through AWS Fargate, the service-worker is identified and registered automatically, but when I deploy it using Amplify, the service-worker is not registered, even though I can access the file when going to the url it's available at.

My withPWA config:

withPWA({ pwa: { dest: 'public', skipWaiting: true, disable: process.env.NODE_ENV === 'development', runtimeCaching, register: true, }, )

Both deployments are successful, but only the fargate one is registering the service worker as it should.

Expected behavior

The amplify deployment should automatically register a service worker when the webapp loads

Reproduction steps

  1. Create next app
  2. Config next-pwa on next.config.js
  3. Deploy the webapp using Amplify
  4. Access deployment url

Build Settings

version: 0.1
backend:
  phases:
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - export BUILD_DIR=$PWD/$BUILD_DIR_NAME
        - yarn install
        - cat aws-exports.js
        - yarn lint --fix
        - export NODE_OPTIONS="--max-old-space-size=5120"
    build:
      commands:
        - yarn build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

Additional information

Fargate deployment is the production, it's using a custom domain and Amplify deployment is using the default amplify url. I don't think it should matter.

mhamendes commented 3 years ago

Any news on this issue? I'm still to get the service worker to properly install in my webapp...

yehonadav commented 2 years ago

i would also like to see this working, i'd even be happy to assist

yehonadav commented 2 years ago

I managed to solve my issues. Here are some errors I encountered along the way, maybe if you had those you can solve the problem yourself like I did:

after this all i did was following chrome's light house results. image

And I successfully managed to make next-pwa work with my amplify app.

mhamendes commented 2 years ago

I played a little bit with this today and still no luck, but I'm not using only next-pwa, I'm also using other plugins

In the console I don't get any of the errors you got along the way, it just shows that the service worker is not installable in the manifest area in the application menu on dev tools.

This is my next.config.js file:

const withImages = require('next-images');
const withPWA = require('next-pwa');
const runtimeCaching = require('next-pwa/cache');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});
const { withSentryConfig } = require('@sentry/nextjs');
const { i18n } = require('./next-i18next.config');

const moduleExports = withBundleAnalyzer(
  withImages(
    withPWA({
      reactStrictMode: true,
      pwa: {
        dest: 'public',
        register: true,
        skipWaiting: true,
        runtimeCaching,
        buildExcludes: [/middleware-manifest.json$/],
        disable: process.env.NODE_ENV === 'development',
      },
      images: {
        disableStaticImages: true,
      },
      i18n,
    })
  )
);

module.exports =
  process.env.NODE_ENV === 'development'
    ? moduleExports
    : withSentryConfig(moduleExports, {});

and this is my manifest.json file:

{
  "name": "Test",
  "short_name": "Test",
  "theme_color": "#5e35f2",
  "background_color": "#f8f9ff",
  "display": "standalone",
  "orientation": "portrait",
  "start_url": "/",
  "scope": "/",
  "icons": [
    {
      "src": "icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png"
    },
    {
      "src": "icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png"
    },
    {
      "src": "icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "maskable"
    },
    {
      "src": "icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "splash_pages": null
}
YarosalvGoIT commented 4 months ago

Hi. Same problem. Do you have any solution?

When I start project on windows - work fine. On ubuntu - worker don`t register and console is clear.