TypeRocket / core

TypeRocket core source files where all the magic lives.
https://typerocket.com
36 stars 21 forks source link

optimize custom route hook location #59

Closed fadlee closed 4 years ago

fadlee commented 5 years ago

Run custom route as soon as possible, just after user authenticated, before WP doing its request parsing.

kevindees commented 5 years ago

Hey @fadlee

I think we have the feature you are looking for already:

https://github.com/TypeRocket/typerocket/blob/master/config/typerocket.php#L40-L55

fadlee commented 5 years ago

Thanks for the response.

I've try and check the codes for that feature. Its not the problem I'm trying to solve.

The _instant_ setting is just for loading the route file, detect routes and initialize the hooks, when to execute the routes. Not when the routes really executed.

Currently, the routes executed on template_include hook.

From my test, I see that will be performance issue, because we execute the routes after WP parse the request, do query to database to check existing post/page for current request.

By defining custom route, I think we don't need WP to do its request parsing. CMIIW.

By executing the routes on init hook, it will be less query to database, and sure better performance.

From my test, at least I can eliminate 3 unneeded query to database.

SELECT ID, post_name, post_parent, post_type FROM wp_posts WHERE post_name IN ('test') AND post_type IN ('page','attachment')
SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND (wp_posts.ID = '0') AND wp_posts.post_type = 'page'  ORDER BY wp_posts.post_date DESC 
SELECT ID FROM wp_posts WHERE post_name LIKE 'test%' AND post_type IN ('post', 'page', 'attachment') AND post_status = 'publish'
fadlee commented 5 years ago

BTW, I'm running TypeRocket in root mode.

I'm defining route in /routes.php:

tr_route()->get()->match('test')->do(function() {
    print_r($GLOBALS['wpdb']);
});
kevindees commented 5 years ago

Hey @fadlee

Thanks for pointing this out. I had used a similar solution a while back but it causes the admin bar to be removed on custom routes. Though what you did find was a bug I needed to fix; the system should not be running all of those queries. I believe this should do the trick... let me know if I'm wrong.

https://github.com/TypeRocket/core/releases/tag/v4.0.63