How do I setup routing for react in GAE? Directly routing to react-router-dom routes via URL fails in GAE on basic create-react-app?

后端 未结 4 2007
星月不相逢
星月不相逢 2020-12-05 19:03

ANSWER for now

This was tough for me to get exactly right. Very little in the way of guidance via Google. I hope this helps others.

As D

相关标签:
4条回答
  • 2020-12-05 19:41

    I took the answers from Nathan Shephard and Dan Cornilescu and condensed them into the app.yaml below. It seems to be working for my React SPA on GAE Standard. No application server (ex: serve.js, ExpressJS, etc) is necessary.

    env: standard
    runtime: nodejs10
    service: default
    
    handlers:
      - url: /static
        static_dir: build/static
    
      - url: /(.*\.(json|ico|js))$
        static_files: build/\1
        upload: build/.*\.(json|ico|js)$
    
      - url: .*
        static_files: build/index.html
        upload: build/index.html
    
    0 讨论(0)
  • 2020-12-05 19:42

    I spent some time on this and this works perfectly for my React CRA on GAE Standard.

    runtime: nodejs10
    service: bbi-staging
    env: standard
    instance_class: F2
    handlers:
      - url: /(.*\.(gif|media|json|ico|eot|ttf|woff|woff2|png|jpg|css|js))$
        static_files: build/\1
        upload: build/(.*)
      - url: /(.*)
        static_files: build/index.html
        upload: build/index.html
    
    0 讨论(0)
  • 2020-12-05 19:45

    After spending 1 splendid day enjoying with Google App Engine, webpack and app.yaml, here is the configuration that worked for me:

    webpack.js (only the json parts that are relevant, i.e. module's rules):

      {
        test: /\.(png|jpg|gif)$/,
        use: [
        {
          loader: 'url-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'media/',
            limit: 8192
          }
        }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
        {
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'fonts/'
          }
        }
        ]
      },
    

    app.yaml

    runtime: nodejs10 
    handlers:
      - url: /(.*\.(png|ico|jpg|gif))$
        static_files: dist/\1
        upload: dist/.*\.(png|ico|jpg|gif)$
      - url: /fonts/(.*\.(woff|woff2))$
        static_files: dist/fonts/\1
        upload: dist/fonts/.*\.(woff|woff2)$
      - url: /html.js
        static_files: dist/html.js
        upload: dist/html.js
      - url: /javascript.js
        static_files: dist/javascript.js
        upload: dist/javascript.js
      - url: /.*
        static_files: dist/index.html
        upload: dist/index.html
        secure: always
        redirect_http_response_code: 301
      - url: /
        static_dir: dist
    

    Note: my output directory is dist, so make sure you change it to build if that is the case for you

    0 讨论(0)
  • 2020-12-05 20:07

    When a request comes in its URL path is compared against the patterns specified in the handlers section of the app.yaml file, in the order in which they are specified. The first match wins and whatever the matching handler specifies is executed. Once you grasp this there's no more need to guess what's going on.

    So you're making requests to www.test-app.com and www.test-app.com/test, which translate to the / and /test URL paths, respectively.

    With your 1st app.yaml (note that your patterns are identical in it, the 2nd one will never be matched):

    • / matches the 1st pattern, the static build/index.html from your app directory (not a .js file!) will be returned
    • /test doesn't match any pattern, 404 will be returned

    With your 2nd app.yaml:

    • / matches the 2nd pattern, the static build dir will be served (which I presume triggers your node app, but I'm not certain - I'm not a node user)
    • /test matches the 1st pattern, again that static build/index.html file will be returned

    I suspect the build/index.html file from your app directory from where you made your deployment (that entire build dir was uploaded as a static dir) had HTML syntax errors at the time when you made the deployment (that's how it was frozen - it's static), causing the error message you see.

    You might have fixed the local copy of the file since the deployment, which could explain why it appears to be working locally now. If so a re-deployment of the app should fix the problems on GAE as well.

    UPDATE:

    I don't think you'd want a static handler for your build/index.html - it'll always serve whatever content was in that file in your local workspace at deployment time.

    I'd follow the non-static example from the official app.yaml Configuration File (updated with a .* pattern instead of the /.* original one to be certain it matches / as well):

    handlers:
    - url: .*
      script: auto
    

    It might even be possible to drop the handlers section altogether, I don't see one in several Node.js samples repo examples:

    • appengine/datastore/app.standard.yaml
    • appengine/memcached/app.standard.yaml
    • appengine/storage/standard/app.yaml
    0 讨论(0)
提交回复
热议问题