最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to properly serve Flutter Web App with Nginx - Stack Overflow

programmeradmin1浏览0评论

I have built the Flutter Web App and everything worked well in dev mode. Now it's time to deploy to production and as a result I have packaged it into a docker image and I'm trying to serve it with nginx using the following nginx.conf file:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name localhost;

        root /usr/share/nginx/html;
        index index.html;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /health {
            access_log off;
            return 200 'OK';
        }

        location /.well-known/apple-app-site-association {
            try_files /.well-known/apple-app-site-association.json =404;
        }

        location /.well-known/apple-app-site-association.json {
            try_files /.well-known/apple-app-site-association.json =404;
        }

        location /assetlinks.json {
            try_files /assetlinks.json =404;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }
}

After successful deployment I started seeing this error in production

Format Exception: SyntaxError: Unexpected token '<',"<!DOCTYPE"... is not a valid JSON

After careful observation I suspect that the above error is coming from this part of my code that loads the translation assets for easy_localisation:

runApp(
    EasyLocalization(
      fallbackLocale: const Locale('en'),
      supportedLocales: const [Locale('en'), Locale('es')],
      path: "assets/translations",
      child: const ProviderScope(child: App()),
    ),
  );

It seems easy localization was unable to load the assets in production even though they were copied over to the docker image.

Here is my Dockerfile and to the best of my knowledge I think I copied over everything properly after running flutter web build --release:

FROM nginx:latest

WORKDIR /usr/share/nginx/html

# Remove the default Nginx static assets
RUN rm -rf ./*

COPY app/build/web /usr/share/nginx/html

COPY nginx.conf /etc/nginx/nginx.conf

RUN chmod -R 755 /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

Also after the copy over I noticed that flutter build web --release command was moving my assets to assets/assets. Which means translations are going to sit in assets/assets/translations. I am suspecting that to be an another issue but I can't conclude yet.

What could be the issue? What can I do to make this work?

发布评论

评论列表(0)

  1. 暂无评论