0

I have app engine an app with some static pages.

I want to serve my static content, and a script. Finally, I want to serve 404 for everything else.

My app.yaml file looks something like

runtime: go113

handlers:
- url: /myscripy
  script: auto

- url: /$
  static_files: public/index.html
  secure: always
  upload: public/index.html

- url: /(.*/)?$
  static_files: public/\1index.html
  secure: always
  upload: public/(.*/)?index.html
  http_headers:
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    X-Frame-Options: SAMEORIGIN

OK, that's working except a few things

  1. it's unclear to me why the - url: /$ is necessary, why is it not subsumed by - url: /(.*/)?$, but that's just a side note.

  2. main problem: any URL that matches nothing will be rendered as empty file with a 200 status code instead of a 404.

  3. I can force 404s by adding something like

- url: /.*
  static_files: public/some_non_existing_file.html
  secure: always
  upload: public/some_non_existing_file.html

at the end of the app.yaml file, but that triggers an ugly 404 last ditch page that is not very appealing.

  1. I can serve nice 404 custom pages like
- url: /.*
  secure: always
  static_files: public/404.html
  upload: public/404.html
  http_headers:
    Status: 404

where public/404.html exists, however the Status: 200 header is sent before the Status: 404 header, making the whole response a 200.

  1. I had hopes for
error_handlers:
  - file: 404.html

to do something for me, but no, it seems to not do anything at all.

  1. I can of course add a custom script to create the 404 response, but it feels a bit silly to me. Isn't there a better way ?

I'm a bit stumped - I can't believe that a long standing industry tool like GAE has no way to statically define a custom 404 page, or even render 404 status code for nonexisting pages.

Really appreciate any advice, thanks in advance!

1 Answers1

1

Indeed, there is not easier way for you to achieve the custom 404 page, then adding a custom script to create the 404 response. App Engine, unfortunately, still doesn't work very well with handling some errors, which includes the error 404. As you can check in this official documentation here, there is no custom handlers from this error on Java.

This is just an example that a custom script is the viable solution.Otherwise, the solutions won't return the error 404 - as you well mentioned - or you would need to use a catch-all handler, which is not very recommended as well, since it's too broad of a solution.

In case you are using Python, there is a very good example of how to configure the trigger and example of script here.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • Thanks, Gabriel. I thought so, too, but I still can't believe the default is to serve nonexisting pages as empty 200-status pages. That's so ... harebrained, for lack of a better word. I'll hold off marking the question as definitely answered -- I still hope someone from the GAE team can give a really good explanation. – Klapaucius Klapaucius Jul 23 '20 at 22:35
  • BTW - why I find this harebrained: there are many articles on the web suggesting to use GAE to host static websites. Not having valid 404s for non-existing pages seems to be a non-starter for anything that should be picked up by webcrawlers. Why does noone else point this out ? – Klapaucius Klapaucius Jul 24 '20 at 00:22
  • Sure, I understand your point @KlapauciusKlapaucius , maybe it's worht it to open a feature request in [Google's Issue Tracker](https://cloud.google.com/support/docs/issue-trackers), so they can check about the possibility of implementing this in the future. – gso_gabriel Jul 24 '20 at 05:26