Making WordPress.org

Changeset 13071


Ignore:
Timestamp:
12/19/2023 07:07:11 AM (12 months ago)
Author:
dd32
Message:

Plugin Directory: Downloads: Try a 302 redirect on the latest-stable.zip url to ensure browsers don't cache a stale version when using blueprints.

The playground ran into an issue that the .latest-stable.zip endpoint didn't expose any caching headers, which caused browsers to more aggressively cache the response, leading to previews loading previous versions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php

    r8484 r13071  
    1717        try {
    1818            $request = $this->determine_request();
     19
     20            $this->maybe_redirect_latest_stable( $request );
    1921
    2022            $this->serve_zip( $request );
     
    5658            $version = $m['version'];
    5759        }
    58         if ( 'latest-stable' == $version ) {
     60
     61        // If the latest-stable is requested, determine the file to serve.
     62        $is_latest_stable = ( 'latest-stable' == $version );
     63        if ( $is_latest_stable ) {
    5964            $version = $this->get_stable_tag( $slug );
    6065        }
     
    7984        }
    8085
    81         return compact( 'zip', 'slug', 'version', 'args', 'checksum_request', 'signature_request' );
     86
     87        return compact( 'zip', 'slug', 'version', 'args', 'checksum_request', 'signature_request', 'is_latest_stable' );
     88    }
     89
     90    /**
     91     * Redirect to the latest stable version if requested.
     92     *
     93     * @param array $request The request array for the request.
     94     */
     95    protected function maybe_redirect_latest_stable( $request ) {
     96        if ( ! $request['is_latest_stable'] ) {
     97            return;
     98        }
     99
     100        $file     = $this->get_file( $request );
     101        $redirect = 'https://downloads.wordpress.org/plugin/' . basename( $file );
     102
     103        if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
     104            $redirect .= '?' . $_SERVER['QUERY_STRING'];
     105        }
     106
     107        // CORS, to match the ZIP passthrough.
     108        header( 'Access-Control-Allow-Methods: GET, HEAD' );
     109        header( 'Access-Control-Allow-Origin: *' );
     110
     111        // Tell browsers to only cache this for 5 minutes.
     112        header( 'Cache-Control: max-age=300' );
     113
     114        // Redirect to the file they want.
     115        header( 'Location: ' . $redirect, 302 );
     116        exit;
    82117    }
    83118
Note: See TracChangeset for help on using the changeset viewer.