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

feat: upload blobs on onDev event #5552

Conversation

ndhoule
Copy link
Contributor

@ndhoule ndhoule commented Mar 20, 2024

This changeset updates the dev timeline's steps to run the uploadBlobs core plugin on the onDev event. (It will also allow users to use file- based blobs locally using netlify dev, but that's an ancillary benefit.)

Some background on the "why" of this issue: Frameworks has been exploring use cases that involve writing to the blobs directory (for example, a Remix site that writes an initial cache static files into the blobs directory that may later be invalidated and replaced with dynamically generated and served assets). They gave the feedback that it's difficult to test out this type of functionality locally, where netlify dev is their primary workflow.

I'm not married to this particular implementation, but this seemed like the simplest way to accomplish it. Also, I wasn't sure where would be best to add tests for this functionality--happy to add them with some direction!

Fixes CT-651.

@ndhoule ndhoule force-pushed the nathanhoule/ct-651-blobs-client-should-read-from-file-based-upload-folder branch from 571c54f to 2d2f1da Compare March 20, 2024 17:39
@ndhoule ndhoule marked this pull request as ready for review March 20, 2024 17:48
@ndhoule ndhoule requested review from a team as code owners March 20, 2024 17:48
@ndhoule
Copy link
Contributor Author

ndhoule commented Mar 20, 2024

Anyone can review this, but tagging @eduardoboucas as a reviewer because we discussed this functionality.

Seems like the failing test check is flaky on main, but let me know if that's not the case and I can dig.

...steps,
// Trigger the uploadBlobs step during development to allow users to test file-based blob
// uploads locally and to allow frameworks to capture file-based blobs written by frameworks
{ ...uploadBlobs, event: 'onDev' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you're reusing the core step we have for builds and just changing the event property. As far as I know, we don't really have a precedent for running the same core step in multiple events, but I would probably have two separate core steps — one for dev, one for build — that may reuse common functionality. I think taking a core step and creating another one from it on-the-fly here might be confusing to people.

More importantly, if we run the existing core step as is as part of dev, won't we be uploading blobs to the actual production store? How do we ensure that we're uploading blobs to the local store?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you're reusing the core step we have for builds and just changing the event property. As far as I know, we don't really have a precedent for running the same core step in multiple events, but I would probably have two separate core steps — one for dev, one for build — that may reuse common functionality. I think taking a core step and creating another one from it on-the-fly here might be confusing to people.

Sure! I can split this into a dev_blobs_upload core step that uses shared code.

More importantly, if we run the existing core step as is as part of dev, won't we be uploading blobs to the actual production store? How do we ensure that we're uploading blobs to the local store?

As I understand it, the answer to your first question is no: The blobs client used in the upload blobs step picks up the local blob server's settings via process.env['NETLIFY_BLOBS_CONTEXT'], which is set by both the dev and serve commands. (ntl serve actually already uploads blobs to the local blob server: netlify/cli#6299.)

To your second question: I think this concern would apply equally to serve, right? I'm not opposed to adding some checks to ensure serve and dev don't accidentally upload blobs to production, but given that I think we'd already have seen this issue come up in serve, I'm inclined to solve it separately. What do you think?

@ndhoule ndhoule force-pushed the nathanhoule/ct-651-blobs-client-should-read-from-file-based-upload-folder branch from 2d2f1da to 1086316 Compare March 28, 2024 17:21
@ndhoule
Copy link
Contributor Author

ndhoule commented Mar 28, 2024

@eduardoboucas Just pushed up the changes we discussed--let me know what you think!

const {
constants: { IS_LOCAL },
} = args[0]
return IS_LOCAL && ((await uploadBlobs.condition?.(...args)) ?? true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think IS_LOCAL is a particularly useful check given that I wouldn't expect us to run dev anywhere but locally, but it doesn't seem to hurt to perform this check, I guess? I can remove it if you agree it's not useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not doing it for other core steps running on the dev timeline, so for consistency we should probably remove it.

eduardoboucas
eduardoboucas previously approved these changes Mar 29, 2024
const {
constants: { IS_LOCAL },
} = args[0]
return IS_LOCAL && ((await uploadBlobs.condition?.(...args)) ?? true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not doing it for other core steps running on the dev timeline, so for consistency we should probably remove it.


export const devUploadBlobs: CoreStep = {
event: 'onDev',
coreStep: uploadBlobs.coreStep,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should've made that clearer, but what I meant by "reuse common functionality" #5552 (comment) was moving shared logic into a separate file that both core steps would use.

In this case, you're importing one core step into the other and reusing the logic it implements. There's no issue with that functionality-wise, but I worry that future us will be changing one core step unaware that it's actually affecting another core step that imports it.

I'm approving because I don't want to block you, but I'd recommend revisiting this if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up splitting some more common functionality out into the utils/blobs.ts file and otherwise duplicated most of the contents of upload_blobs to dev_upload_blobs.

In general I share your concern, but in practice, it seems like the two steps should behave identically right now. Given that, I'm not sure how best to split out any more common functionality without simply moving the entire core step into a shared file.

I'm going to merge this as it stands so we can address the issue this solves, but if you have suggestions on how we might better structure this, let me know--happy to address it in a follow-up PR!

This changeset updates the dev timeline's steps to run the `uploadBlobs`
core plugin on the `onDev` event. (It will also allow users to use file-
based blobs locally using `netlify dev`, but that's an ancillary benefit.)

Some background on the "why" of this issue: Frameworks has been
exploring use cases that involve writing to the blobs directory (for
example, a Remix site that writes an initial cache static files into the
blobs directory that may later be invalidated and replaced with
dynamically generated and served assets). They gave the feedback that
it's difficult to test out this type of functionality locally, where
`netlify dev` is their primary workflow.

Fixes [CT-651](https://linear.app/netlify/issue/CT-651).
@ndhoule ndhoule force-pushed the nathanhoule/ct-651-blobs-client-should-read-from-file-based-upload-folder branch from 85a607c to 67ba719 Compare April 1, 2024 21:05
This shares some, but not all, functionality between the two tasks. We
should figure out how to better share functionality between the two jobs
but this should be fine for now.
@ndhoule ndhoule force-pushed the nathanhoule/ct-651-blobs-client-should-read-from-file-based-upload-folder branch from 67ba719 to da6a5eb Compare April 1, 2024 21:10
@ndhoule ndhoule enabled auto-merge (squash) April 1, 2024 21:41
@ndhoule
Copy link
Contributor Author

ndhoule commented Apr 1, 2024

@eduardoboucas Looks like I need a re-review to merge 🙏

@ndhoule ndhoule merged commit 9b8ed35 into main Apr 3, 2024
36 checks passed
@ndhoule ndhoule deleted the nathanhoule/ct-651-blobs-client-should-read-from-file-based-upload-folder branch April 3, 2024 20:54
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

Successfully merging this pull request may close these issues.

None yet

3 participants