Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -36,6 +36,7 @@
"@php-wasm/universal": "0.5.4",
"@php-wasm/web": "0.5.4",
"@uiw/react-codemirror": "^4.21.20",
"@webcontainer/env": "1.1.0",
"@wp-playground/blueprints": "0.5.4",
"@wp-playground/client": "0.5.4",
"classnames": "^2.3.2",
Expand Down
10 changes: 10 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,15 @@ async function getAbsoluteURL() {
if (isGitHubCodespace) {
return getCodeSpaceURL(port);
}
if (isWebContainer()) {
return (
HostURL.parse('http://localhost:' + port)
.toString()
// Correct the bug in HostURL.parse – it should return https://
// instead of http://
.replace('http://', 'https://')
);
}

if (absoluteUrlFromBlueprint) {
return absoluteUrlFromBlueprint;
Expand Down
12 changes: 12 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 @@ -103,6 +104,17 @@ export async function startServer(
),
body: body as string,
};

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).hostname;
data.headers['origin'] = options.absoluteUrl;
}
const resp = await php.request(data);
res.statusCode = resp.httpStatusCode;
Object.keys(resp.headers).forEach((key) => {
Expand Down