Skip to content

Commit

Permalink
wp-now: WebContainer support fixes (#221)
Browse files Browse the repository at this point in the history
Improvements to #150
with fixes related to updates for wp-now in WebContainer

Can be retargetting to `trunk` if you want to close the other PR

## Testing Instructions

Visit https://stackblitz.com/edit/node and run 

```bash
npx @wp-now/wp-now@some-version start
```

WP-Now should load in your browser
  • Loading branch information
kirjavascript authored Apr 15, 2024
1 parent 8cfa19f commit 3a3a4e1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@php-wasm/universal": "0.6.13",
"@php-wasm/web": "0.6.13",
"@uiw/react-codemirror": "^4.21.20",
"@webcontainer/env": "1.1.1",
"@wp-playground/blueprints": "0.6.13",
"classnames": "^2.3.2",
"comlink": "^4.4.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/wp-now/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { portFinder } from './port-finder';
import { isValidWordPressVersion } from './wp-playground-wordpress';
import getWpNowPath from './get-wp-now-path';
import { DEFAULT_PHP_VERSION, DEFAULT_WORDPRESS_VERSION } from './constants';
import { isWebContainer, HostURL } from '@webcontainer/env';

export interface CliOptions {
php?: string;
Expand Down Expand Up @@ -75,6 +76,9 @@ async function getAbsoluteURL() {
if (isGitHubCodespace) {
return getCodeSpaceURL(port);
}
if (isWebContainer()) {
return HostURL.parse('http://localhost:' + port).toString();
}

if (absoluteUrlFromBlueprint) {
return absoluteUrlFromBlueprint;
Expand Down
13 changes: 13 additions & 0 deletions packages/wp-now/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import compressible from 'compressible';
import fileUpload from 'express-fileupload';
import { portFinder } from './port-finder';
import { NodePHP } from '@php-wasm/node';
import { isWebContainer } from '@webcontainer/env';
import startWPNow from './wp-now';
import { output } from './output';
import { addTrailingSlash } from './add-trailing-slash';
Expand Down Expand Up @@ -79,6 +80,18 @@ export async function startServer(
method: req.method as HTTPMethod,
body,
};

if (isWebContainer()) {
// Unlike a typical Nginx or reverse proxy setup, WebContainers
// overwrite the Host header sent by the browser with a localhost
// URL. However, WordPress detects when the Host header is different
// from the stored site URL and redirects back to the site URL.
// For WordPress to work, we need to make sure the host and origin
// headers contain the public-facing site URL.
data.headers['host'] = new URL(options.absoluteUrl).host;
data.headers['origin'] = options.absoluteUrl;
}

const resp = await php.request(data);
res.statusCode = resp.httpStatusCode;
Object.keys(resp.headers).forEach((key) => {
Expand Down

0 comments on commit 3a3a4e1

Please sign in to comment.