I don’t disagree with you, as this can be considered a suffix
of the URL. But when we “inherit” concepts from WordPress, we always have to try to stay as close as possible to the names already defined in WordPress itself, to avoid confusion.
There is already a filter in WordPress which calls this prefix
: https://developer.wordpress.org/reference/functions/rest_get_url_prefix/. It also makes sense, because it’s also the prefix of the URI: [wp-json]/wp/v2/...
. So I would stick to prefix
because of that.
I really like that idea. It’ll solve the problem very elegantly.
I guess new features don’t need to be backward compatible with state.source.api
, because when we instruct people to set state.router.redirections
when can ask people to also set state.source.url
, right? But we have to remember to put that requirement in the docs.
I really like @juanma idea. If state.source.url
is not defined, it fallbacks to state.frontity.url
.
My feedback about the proposal:
1. Move state.source.prefix
to state.wpSource.prefix
This is in preparation for Source v2, where we want packages from different APIs, like the REST API and GraphQL, to coexist.
const mergedState = {
source: {
url: "https://mybackend.com",
},
wpSource: {
prefix: "/wp-json",
},
wpGraphqlSource: {
prefix: "/graphql",
},
};
I know that state.source.api
is still coupled to wpSource
, but that’s something we cannot change now to keep backward compatibility.
2. If state.source.url
is missing, use state.frontity.url
As Juanma suggested, we can ask people to delete state.source.url
if they are using the Embedded mode, and it will fall back to state.frontity.url
.
3. Support for ?rest_route=
, but using the prefix
field
That’s a good point, thanks for adding it here. My only opinion is that I don’t see a reason why people should not set it in the prefix, instead of setting it to false
:
const settings = {
state: {
wpSource: {
prefix: "?rest_route=",
},
},
};
We have to make sure that it works in libraries.source.api.get
though.
4. Keep backward compatibility with WordPress.com
We have to make sure that we keep backward compatibility with WordPress.com. Right now, state.source.isWpCom
is populated automatically.
Juanma made a series of tests a while ago and discovered that the long public-api URL (https://public-api.wordpress.com/wp/v2/sites/site.wordpress.com) is only used for free sites that use the site.wordpress.com URL.
Thanks to that, I think we can still detect it and keep backward compatibility.
export default {
state: {
wpSource: {
prefix: "/wp-json",
},
source: {
// The default when `state.source.url` is not overwritten in
// frontity.settings.js is to fallback to `state.frontity.url`.
url: ({ state }) => state.frontity.url,
// Keep backward compatibility when `state.source.api` is not
// overwritten in frontity.settings.js.
api: ({ state }) => {
// Check if it's a free WordPress.com site.
if (/^https:\/\/[\w-]\.wordpress\.com/.test(state.source.url))
return `https://public-api.wordpress.com/wp/v2/sites/${state.source.url}`;
return normalize(state.source.url + state.wpSource.prefix);
},
// Keep backward compatibility.
isWpCom: ({ state }) =>
state.source.api.startsWith("https://public-api.wordpress.com"),
},
},
};