Google App Engine: Routing which appends .html - reactjs

I'm deploying a React app to Google App Engine. Currently I need to append .html or /index.html to the requests but I would like to avoid this so I have clean routes.
For example, I can load mywebsite.com/docs/page1.html and mywebsite.com/docs/page1/index.html but I want to load mywebsite.com/docs/page1. The react app actually builds both of these duplicate pages so it appears this is somewhat default behaviour. The routing is only "broken" like this when I build, it works fine in the dev server.
Directory structure
- build/my_docs/
- css/
- img/
- js/
- docs/
- page1.html
- page1/
- index.html
app.yaml
runtime: python3
service: docs
handlers:
- url: /
secure: always
redirect_http_response_code: 301
static_files: build/my_docs/index.html
upload: build/my_docs/index.html
- url: /css/(.*)
static_files: build/my_docs/css/\1
upload: build/my_docs/css/(.*)
- url: /img/(.*)
static_files: build/my_docs/img/\1
upload: build/my_docs/img/(.*)
- url: /js/(.*)
static_files: build/my_docs/s/\1
upload: build/my_docs/js/(.*)
- url: /(.*\.(json|ico|xml))$
static_files: build/my_docs/\1
upload: build/my_docs/.*\.(json|ico|xml)$
- url: /docs/(.*)
static_files: build/my_docs/docs/\1
upload: build/my_docs/docs/(.*).html
# Trying to append .html to non css/img/js requests but it doesn't work!
I also tried to deploy the sample Docusaurus project to GAE with no app.yaml handlers, and I see the same undesired routing (need to append .html).

Are you satisfied with serving these html files as static docs, or should they be templates which you can pass content to?
First, in your / handler, there is no such file build/my_docs/index.html
Your /docs/(.*) handler appears to be fine. You say it works in dev but not in build? That sounds like a routing problem inside React. Check your React routing. Also, make sure you're resetting your browser cache frequently, as you may be caching old React code.
** EDIT **
Let's look at this handler:
- url: /docs/(.*)
static_files: build/my_docs/docs/\1
upload: build/my_docs/docs/(.*).html
If you request at the url /docs/page1, it is going to try to load the static file from the directory build/my_docs/docs/page1. So far, so good, as that directory exists.
The problem comes in the next line. You are asking to serve the static file build/my_docs/docs/page1.html, which does not exist in the directory specified. Try one of these:
- url: /docs/(.*)
static_files: build/my_docs/docs/\1
upload: build/my_docs/docs/(.*)/index.html
or:
- url: /docs/(.*)
static_files: build/my_docs/docs
upload: build/my_docs/docs/(.*).html
I other words, there is a mismatch between the static_files directory you specify and the location of the file you're trying to serve.

Related

issues with deploying hugo site to google cloud app engine

So I'm trying to deploy my Hugo blog to google cloud app engine, but I'm having issue with the URL mapping,I have played with it a lot but nothing seem to work
The home page is working and I can see the the posts but when I click on a post I got error: Not Found message
app.yaml
env: standard
runtime: nodejs16
service: default
handlers:
- url: /
static_files: public/index.html
upload: public/index.html
- url: /posts
static_dir: public/posts
- url: /(.*)
static_files: public/\1
upload: public/(.*)
Files structure
website link: https://www.waelfadlallah.com/
Thank to NoCommandLine the issue is resolved, here is how my app.yaml looks like now
env: standard
runtime: nodejs16
service: default
handlers:
- url: /posts/(.+)/
static_files: public/posts/\1/index.html
upload: public/posts/(.+)/index.html
- url: /assets
static_dir: public/assets
- url: /
static_files: public/index.html
upload: public/index.html
See if this works....
Instead of using static_dir, try using static_files and have your handler like the example here. That example has (the sample below). See if you can modify it to suit your pattern
- url: /([^\.]+)([^/])
static_files: www/\1\2/index.html
upload: www/(.+)

App engine is not serving images of public folder in react

I hosted a react app in app engine , and i have a lot of pictures in the public folder how are displayed correctly in my local machine , but when i deploy it in standard environnement , all the files are deployed , i checked the sources in url images and he serve them but they seems corrupted (when i download them) maybe i did something wrong in the configuration ? the only thing that i can acess is the favicon . this is what i get in source .
and my app.yaml is :
runtime: nodejs12
service: default
instance_class: F4_1G
handlers:
- url: /static
static_dir: build/static
- url: /assets
static_dir: build/assets
- url: /(.*\.(json|ico|js))$
static_files: build/\1
upload: build/.*\.(json|ico|js)$
- url: .*
static_files: build/index.html
upload: build/index.html
Your app.yaml does not have a handler for /media. So the GCP returns index.html when accessing viper8.JPG. Add handler for /media.

Nuxt static site on GAE 404 on page refresh

I know there are already several posts on this topic, but I did not get this thing to work with the recommended settings. When I generate and start my Nuxt static site locally with the following commands, everything works fine. Even when I refresh the page, the same route is displayed.
nuxt generate && nuxt start
When I deploy my dist folder to Google App Engine, the site seems to work like a charm. However, as soon if I hit the refresh button, a 404 is displayed. My app.yaml looks as follows:
---
runtime: python37
instance_class: F1
handlers:
- url: /
static_files: index.html
upload: index.html
secure: always
- url: /(.*)
static_files: \1
upload: (.*)
secure: always
Applicable nuxt.config.js settings:
ssr: true
target: 'static'
Managed to fix this in the app.yaml, but not 100% sure why this happened. It probably has to do with the "catch-all" handler on the end. Here is my working example:
---
runtime: python37
instance_class: F1
handlers:
- url: /(.*\..+)$
static_files: \1
upload: (.*\..+)$
- url: /.*
static_files: index.html
upload: index.html

App Engine app.yaml handlers not working as expected

I am trying to set up a vue.js application with the vue router set in "history" mode. I also want to serve my back end APIs from the same app engine application. (e.g., if a user navigates to /some/path/in/the/app and refreshes the page or shares the link, the application will display the expected page)
here is my app.yaml:
runtime: python37
handlers:
- url: /api/.*
secure: always
script: auto
- url: /css
static_dir: www/css
- url: /js
static_dir: www/js
- url: /semantic
static_dir: www/semantic
- url: /img
static_dir: www/img
- url: /
static_files: www/index.html
upload: www/index.html
- url: /.*
static_files: www/index.html
upload: www/index.html
When I try to hit any of my api endpoints, the static index.html file is served instead of the response from the endpoint.
If the last route is omitted (url: /.*), then the api endpoints are served correctly, but the Vue.js app can only be entered from the "/" route and the deep links in the application do not work as expected
I am not looking to have a script in my application to serve the a static file.
note, this question is similar, but none of the answers addressed my situation:
AppEngine app.yaml config for single page apps
The trick was to follow a microservice architecture and split my application into two modules and define the routes in a dispatch.yaml file.
There is a sample project here:
App Engine Modules sample project

Where do I put my sitemap on appengine?

I know for typical static file such as stylesheets app.yaml must contain the path to the directory holding said files. E.g.
- url: /stylesheets
static_dir: stylesheets
How do I go about adding a sitemap to GAE server?
this is what i did for my favicon, since it's a static file:
- url: /favicon.ico
static_files: media/img/favicon.ico
upload: media/img/favicon.ico
the xml sitemap is a static file too, so you may want to do the same:
- url: /sitemap.xml
static_files: my/folder/path/sitemap.xml
upload: my/folder/path/sitemap.xml
not completely sure about the upload line, tho. but my favicon didn't work in the first place without it
edit: if you want a dynamic sitemap, you could follow the instructions in this link to generate them everytime the sitemap page gets visited
In order to follow the best practices on App Engine I would recommend you to check this Boilerplate http://appengine.beecoss.com
Over there you will find where put sitemap.xml and many more files you need. This is the best way to do it.
- url: /(robots\.txt|humans\.txt|crossdomain\.xml|sitemap\.xml)
static_files: static/\1
upload: static/(robots\.txt|humans\.txt|crossdomain\.xml|sitemap\.xml)

Resources