GatsbyJs client only paths goes to 404 page when the url is directly accessed in browser in “production”

前端 未结 4 1735
谎友^
谎友^ 2020-12-28 15:44

I have created a Gatsby app and configured gatsby-node.js to a create client only paths, which are all working fine in development while directly accessing the

4条回答
  •  太阳男子
    2020-12-28 16:32

    For anyone who uses S3 (gatsby-plugin-s3) and CloudFront, I've resolve 302/404 by adding generateMatchPathRewrites: false in the gatsby-plugin-s3 config and creating a Lambd@Edge function for origin request with code below:

    exports.handler = async (event) => {
      const request = event.Records[0].cf.request;
      if (/^\/app\//i.test(request.uri)) {
          request.uri = '/app/index.html';
      }
      return request;
    };
    

    Though 302/404 went away, I still have the issue when doing a hard refresh. For example when I am at /app/page2 and hit refresh, the default component in /app will load then half a second later the component in /app/page2 will show up but in a weird way. Some css classes of /app is mixed in /app/page2. If anyone has any idea about this, please let me know.

提交回复
热议问题