-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Client-side API #353
Client-side API #353
Conversation
This is a 12% increase to our first-interaction bundle. Is there anything that can be done to avoid this? What does usage look like? How does the user know when they can make calls to the API? |
@jakearchibald PTAL! Lazy-loading the API now :) I also updated the example batch app, which actually looks really nice in terms of code now. I’d love to bundle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming along nicely. I agree that we should use this for the share target stuff.
We need to decide if the API is solely for computation, or if it's a UI driver. From this PR it seems like the latter, and I think that's the right direction. That works for things like a share target API, it works for creating a glitch that opens a particular image with particular settings (once we expand the API to include settings). But that means the API needs to cope with user interactions that might happen during its driving, so calls should I don't have a strong opinion if this is done with events, or promises stored on the instance. Some things to think about: If the user changes some compression settings on the left, then the API changes settings on the right, should the API returned promise wait on both sides to finish compressing, or just the right? If this is a UI driver, should we separate the setting of things from the completion of things? Eg, if I What's our backwards compatibility story here? Should the API involve some sort of version check? We could change our major version when the API breaks compatibility. |
The more I think about it, the more I agree it’s what we should aim for.
I’ll see if I can add a got way to add that.
I don’t think it should.
It’s mostly how it works right now, but I think you are right that more explicit events (?) for intermediate steps would be nice.
It’s probably a good call to add a the major version to the handshake and just abort on mismatch. |
I'm leaning towards promises being better, but I'm not awfully sure. After driving the UI, you could get a promise for "image decoded" (which is when you could get width/height & format if we offered it), and a promise for the complete encoding of a particular side. Edit: When I say "get a promise", I mean it'd be an additional call to get that promise. The methods themselves would resolve once the state is set.
Error I think. |
@jakearchibald PTAL |
@jakearchibald PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caught a few more things.
Also, there should be docs for this, which we can link to from the main README.
@jakearchibald PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only nits.
Add some docs and a demo, and we're good to go I think.
0c805c4
to
57eb2dc
Compare
This is ready for final review and hopefully a merge :D I pushed the small batch processing demo to a glitch, using the real SDK file exposed from this PR endpoint. @jakearchibald PTAL |
package.json
Outdated
@@ -4,9 +4,10 @@ | |||
"version": "1.3.3", | |||
"license": "apache-2.0", | |||
"scripts": { | |||
"build:sdk": "microbundle --compress -f es -o build -i src/sdk.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think --compress
is the default and -i
is optional.
"build:sdk": "microbundle --compress -f es -o build -i src/sdk.ts", | |
"build:sdk": "microbundle -f es -o build src/sdk.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be adding a load of definition files into the build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Do you think that’s a problem? There’s technically no harm in emitting them and I have no easy way to teach microbundle
to not do that.
@jakearchibald ping :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this create a privacy problem?
Eg, evil.com
contains a link "Check out squoosh.app!" which, when clicked, opens a new window. To the user it looks like a regular tab with "squoosh.app" in the address bar, but if they drop any private images onto the site, evil.com
can read it.
It seems like we need to limit usage of this API to cases where the source of control is obvious, eg iframes, as the URL bar will display evil.com
rather than squoosh.app
.
# Module imports need CORS, so we enable it for the SDK file. | ||
/squoosh.mjs | ||
Access-Control-Allow-Origin: * | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the header to all responses. May as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This resource has a max-age of 31536000. Feels like it should be no-cache right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, what's the benefit of self-hosting this rather than providing it as a separate package? I'm worried about making changes to this resource that breaks people's sites.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that this way at least SDK and Squoosh stay in sync in terms of versioning. We can also not host it and publish it as a package, but not sure it’s that different in terms of breakage.
package.json
Outdated
@@ -4,9 +4,10 @@ | |||
"version": "1.3.3", | |||
"license": "apache-2.0", | |||
"scripts": { | |||
"build:sdk": "microbundle --compress -f es -o build -i src/sdk.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be adding a load of definition files into the build.
}`, | ||
); | ||
} | ||
ev.stopPropagation(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we already discussed this, but what's the benefit here? What other kind of listeners do we expect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was that there could be a scenario where an squoosh iframe is reused and the next user might send a new READY? message. It’s mostly me playing it safe.
Hopefully this limits it to iframes only. |
Is this blocked on something? |
Hi guys, |
Great PR! This is an example of integration with a CMS created based on this PR. I hope this PR will be merged into master. |
Any context on why this was closed? |
Pretty sure this was due to moving the branch |
Pretty sure you can just select a new branch for the PR without needing to close it |
Changing the master branch on GitHub auto-closes all PR's targeted at that branch. |
Closing this for now in favor of the CLI for batch processing (and also Share Target fulfills most of the purpose). |
Can Share Target be used to programmatically compress images client side (without user interaction)? |
Here’s the current, rather hacky implementation of the client-side API so we can start discussing the implementation approach.
The basic approach is to define an API and expose it to
window.parent
via Comlink, this way squoosh can be embedded using<iframe>
and the API methods can just be invoked.Here’s the code for an example batch processing app.