Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request Handler: Serve static files if they exist in VFS. Don't guess. #702

Merged
merged 2 commits into from
Oct 16, 2023

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Oct 16, 2023

Replaces approximate rules such as

if(path.startsWith('/wp-content')) {
    // serve from VFS
} else {
    // serve from remote server
}

With actual file existence check.

Context

When a static file is requested from Playground, the service worker doesn't know whether to serve it from VFS or playground.wordpress.net. It tries to guess that based on approximate rules such as path.startsWith('/wp-content'). This usually works, but is not very reliable. For example, when you store an entirely new WordPress instance in VFS, you would expect all the static assets to be served from VFS, but that is not what the request handler would do.

This does not play well with previewing WordPress Pull Requests as we're actually replacing the entire WordPress instance in VFS with an arbitrary branch.

Related to #700
Closes #109

Testing Instructions

  1. Start Playground npm run start
  2. Use it for a bit, go to wp-admin, confirm that static assets like CSS and JS files are loaded without issues
  3. Upload something, confirm that worked too

Breaking change

The isStaticFilePath option is removed in this PR..

This is a breaking change in public API and may be relevant for wp-now CC @sejas @danielbachhuber.

While that sounds scary, everything should "just work" without major changes required in wp-now, other than removing this usage of isStaticFilePath:

https://github.com/WordPress/playground-tools/blob/d792d7f03323aa0ec34e5885e4aebd5741d96f24/packages/wp-now/src/wp-now.ts#L53-L65

Replaces approximate rules such as

```js
if(path.startsWith('/wp-content')) {
    // serve from VFS
} else {
    // serve from remote server
}
```

With actual file existence check.

When a static file is requested from Playground, the service worker
doesn't know whether to serve it from VFS or playground.wordpress.net.
It tries to guess that based on approximate rules such as
`path.startsWith('/wp-content')`. This usually works, but is not
very reliable. For example, when you store an entirely new WordPress instance
in VFS, you would expect all the static assets to be served from VFS,
but that is not what the request handler would do.

This does not play well with previewing WordPress Pull Requests as we're
actually replacing the entire WordPress instance in VFS with an
arbitrary branch.

Closes #109

1. Start Playground `npm run start`
2. Use it for a bit, go to wp-admin, confirm that static assets like CSS
   and JS files are loaded without issues
3. Upload something, confirm that worked too

CC @sejas @danielbachhuber as this is a breaking change in public API and may be
relevant for wp-now. While that sounds scary, everything should "just work" without
major changes required in wp-now, other than perhaps removing usages of
the `isStaticFilePath` option.
@danielbachhuber
Copy link
Member

This is a breaking change in public API and may be relevant for wp-now

While that sounds scary, everything should "just work" without major changes required in wp-now, other than removing this usage of isStaticFilePath:

Makes sense, thanks for the heads up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static assets of bundled plugins not recognized by service worker to rewrite request
2 participants