nystudio107 / craft-plugin-vite

Plugin Vite is the conduit between Craft CMS plugins and Vite, with manifest.json & HMR support
MIT License
12 stars 11 forks source link

Asset URL generation causing DB writes on the front-end #27

Closed rob-baker-ar closed 4 months ago

rob-baker-ar commented 4 months ago

I was not sure where exactly to report this as I noticed it coming via Retour & SEOMatic, but the common element is the Vite plugin, so here we are!

The bug

On the front-end of a site I have I am seeing DB writes like this:

INSERT INTO `resourcepaths` (`hash`, `path`) VALUES ('7facf4f9', '@nystudio107/retour/web/assets/dist') ON DUPLICATE KEY UPDATE `path`=VALUES(`path`)
INSERT INTO `resourcepaths` (`hash`, `path`) VALUES ('8dc133d6', '@nystudio107/seomatic/web/assets/dist') ON DUPLICATE KEY UPDATE `path`=VALUES(`path`)

Going up the stack, VitePluginService::init() is the common element, specifically, here:

https://github.com/nystudio107/craft-plugin-vite/blob/4ff8dfabd51663fcb081f092eeeba1ed2ed88a50/src/services/VitePluginService.php#L67

This will eventually trigger the DB writes.

Background

The "why" for this is that I am trying to reduce the number of DB writes on the front-end of our sites, as due to the fix for this https://github.com/craftcms/cms/issues/12557 long-ish running db backups can block the front-end if there are writes during the backup.

To reproduce

  1. On a site using SEOMatic or Retour (probably others that use the Vite plugin too)
  2. Enable Debug toolbar
  3. Load a front-end URL
  4. Notice DB write queries on every request

Expected behaviour

Some mechanism to prevent the Vite plugin asking Craft for a published asset URL when no Vite plugin functionality is used on the front-end of a site. This could be a setting, config variable or potentially moving the relevant code out of VitePluginService::init().

Versions

khalwat commented 4 months ago

hmmmm. Yeah seems to me that for frontend requests, we could just skip this entirely, since it'll never be used

khalwat commented 4 months ago

Addressed in the above commits.

I just cut a release version too, lmk how you go. Just do:

composer update nystudio107/craft-plugin-vite

...and that should do it

rob-baker-ar commented 4 months ago

In the interests of an update: This seems to have done the trick. Thank you.