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

Custom URL endpoint w/ SPF #432

Open
mAAdhaTTah opened this issue Dec 23, 2016 · 7 comments
Open

Custom URL endpoint w/ SPF #432

mAAdhaTTah opened this issue Dec 23, 2016 · 7 comments

Comments

@mAAdhaTTah
Copy link

I'm considering using spfjs for a WordPress theme, but I'd like to use the WP-API to return the JSON response, rather than adding custom behavior to the standard URLs, which would mean a completely different URL structure than what spfjs expects by default. Is there a config item or hook I can use to modify this?

@RoneyThomas
Copy link

No. spfjs sends query string ?spf=navigate so that in the back end it can decide whether to send the full page or JSON.

@DavidCPhillips
Copy link
Contributor

Well it depends on exactly how different. The SPF identifying (?spf=navigate) is configurable by setting the url-identifier field in the global configuration:

https://github.com/youtube/spfjs/blob/master/src/client/config.js#L44

Since the default identifier starts with the parameters, that can be replaced by any valid suffix. For example, the demo uses '.spf.json' to reference static files:

https://github.com/youtube/spfjs/blob/master/web/assets/scripts/main.js#L142

@mAAdhaTTah
Copy link
Author

No, I'm looking at trying to implement it against a completely different URL endpoint. e.g. navigate to /my-post/ hits endpoint @ /wp-json/theme/spf?slug=my-post or something of that nature.

@nicksay
Copy link
Contributor

nicksay commented Jan 13, 2017

For link clicks, the only currently supported base value is an href on the target element or an ancestor:

spfjs/src/client/nav/nav.js

Lines 176 to 182 in 988b652

var target = spf.nav.getAncestorWithHref_(evt.target, linkEl);
// Ignore clicks on targets without an href.
if (!target) {
spf.debug.debug(' ignoring click without href parent');
return null;
}
return target.href;

However, I think it would be a reasonable extension to allow a data attribute on an element to specify a custom URL, perhaps either data-spf-url or data-spf-href? It would look something like:

<a href="/page-url" data-spf-url="/custom-spf-url">...</a>

@DavidCPhillips Any objections? This would break the tight coupling between content served for static/dynamic navigation, but as long as it's not the default behavior, I think it would be okay.

@mAAdhaTTah
Copy link
Author

For my purposes, I'd prefer being able to handle this on the Javascript side, supplying a function that takes the destination URL and returns the API endpoint URL.

@DavidCPhillips
Copy link
Contributor

I'd be open to a custom data attribute. For the JS side, we could potentially make the spfrequest event have a more complex configurable response. That would address issue #427 as well.

@rviscomi
Copy link
Member

If you've got a service worker, it could intercept /foo?spf=navigate requests and fulfill them with responses from /some/other/handler?page=foo&spf=navigate. Not saying it's any better than customizing SPF URL request templates, but it's an interesting solution.

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

No branches or pull requests

5 participants