Using Embedded mode, how do we set up redirection?

I would like to set up a redirection from Frontity domain to WP domain (with the Embedded Mode plugin installed). But I’m not sure how to achieve this efficiently, while still allowing WP to make successful requests to the Frontity / Node server. A couple of options I had in mind:

  • using state.source.redirections = "all" on Frontity and have WordPress check for certain request header args (I see that we can filter the args sent by WP Embedded Mode plugin using the frontity_embedded_request_args filter).
  • using some sort of redirection provided by the host and check for certain args as well (e.g. Vercel using the has property on its redirection)
  • I don’t think we can check the request from Frontity itself, but with Server Extensibility this might be an option.

Any suggestion would be helpful. Thanks!

Hi @kevin

Frontity uses Koa on the node server, so searching for ways to implement redirection using Koa might turn up something useful for you.

For exampe here’s something that I’ve found that might be relevant.

A nice feature of the embedded mode plugin is that it adds a query to the request from WordPress, which is frontity_embedded=true (see: frontity-embedded/template.php at master · frontity/frontity-embedded · GitHub).

Something you could do is redirect all requests to the NodeJS server if that query doesn’t exist. And the easiest way to do that is by running Nginx as a reverse proxy (check out: How To Set Up a Node.js Application for Production on Ubuntu 16.04 | DigitalOcean).

And to redirect you will need to check the request and rewrite the URL to the new domain when it’s not embedded:

server {
        // ...

        map $request_uri $isEmbedded {
                "[?&]frontity_embedded=true" 1;
                default 0;
        }
        if ($isEmbedded = 0) {
                rewrite ^/$ http://www.wp-domain.com permanent;
                rewrite ^/(.*)$ http://www.wp-domain.com/$1 permanent;
        } 
        location / { 
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        // ...
}

!!! warning, not tested !!!