Skip to content

Allow filtering of inner component URL #5087

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

Merged

Conversation

delawski
Copy link
Collaborator

@delawski delawski commented Jul 23, 2020

It's best to review this PR on a commit-by-commit basis (there are just 4 commits).

Since amp-wp-app-shell.js has been moved to the src folder, it'd be easy to miss changes done in there.

Please also note that each commit message contains additional context and information.


Summary

This PR introduces the following changes:

  • Move amp-wp-app-shell.js to the src folder and compile it as part of the webpack build process (6d6d2af).
  • Revert 6054285 since it was breaking the app shell experience (5fbc277).
  • Add wp.hooks filter that can be used to change the inner component URL format is needed (3b9e07b).
  • Fix eslint issues reported in amp-wp-app-shell after moving it to the src folder (c1cfd21). Due to high complexity, a few errors had to be suppressed and should be worked on as part of separate issues.

Amends #1519

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

delawski added 4 commits July 23, 2020 16:58
Building the app shell JS with webpack allows us to use third-party code
(e.g. @WordPress packages) easily. We also take advantage of the code
minification and babel transpilation.

This is a step towards using @wordpress/hooks package in the app shell
script.
The change to the inner component URL format introduced in 6054285
broke the app shell experience. While URL format (use an URL segment
instead of a query parameter) was changed in the JS implementation, the
back-end PHP handler was still expecting the old format. It resulted in
loading 404 pages instead of actual inner components for requested pages.

The follow up commit will allow altering of the URL format if a plugin
integrator really needs to do so.
In some cases, the plugin integrator may need to alter the format of the
inner component URL.

While it is possible to change the way incoming requests for inner
components are handled on the back-end (PHP), there was no way to change
the URL format of the request on the front-end (JS).

This commit makes the inner component request URL filterable using
wp.hooks. Here's how you could filter the URL in your plugin or theme
so that instead of using a query parameter, a new segment is added to
the URL:

```
if ( ampAppShell && ampAppShell.hooks ) {
	function filterInnerComponentUrl( url, baseUrl, componentQueryVar ) {
		const filteredUrl = new URL( baseUrl );
		const pathSuffix = '_' + componentQueryVar + '_inner';
		filteredUrl.pathname = filteredUrl.pathname + pathSuffix;

		return filteredUrl;
	}

	ampAppShell.hooks.addFilter( 'amp.appShell.innerComponentUrl', 'ampWpAppShell/filterInnerComponentUrl', filterInnerComponentUrl );
}
```
@google-cla google-cla bot added the cla: yes Signed the Google CLA label Jul 23, 2020
@westonruter westonruter merged commit ca3c404 into ampproject:add/spa-amp-shadow-dom Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants