Skip to content

Commit

Permalink
Add jsdocs for navigate and prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Dec 1, 2023
1 parent 736e823 commit 1cf650a
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions packages/interactivity/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ const regionsToVdom = ( dom ) => {
return { regions, title };
};

// Prefetch a page. We store the promise to avoid triggering a second fetch for
// a page if a fetching has already started.
/**
* Prefetchs the page with the passed URL.
*
* The function normalizes the URL and stores internally the fetch promise, to
* avoid triggering a second fetch for an ongoing request.
*
* @param {string} url The page URL.
* @param {Object} [options] Options object.
* @param {boolean} [options.force] Force fetching the URL again.
* @param {string} [options.html] HTML string to be used instead of fetching
* the requested URL.
*/
export const prefetch = ( url, options = {} ) => {
url = cleanUrl( url );
if ( options.force || ! pages.has( url ) ) {
Expand All @@ -84,7 +94,26 @@ const renderRegions = ( page ) => {
// Variable to store the current navigation.
let navigatingTo = '';

// Navigate to a new page.
/**
* Navigates to the specified page.
*
* This function normalizes the passed href, fetchs the page HTML if needed, and
* updates any interactive regions whose contents have changed. It also creates
* a new entry in the browser session history.
*
* @param {string} href The page href.
* @param {Object} [options] Options object.
* @param {boolean} [options.force] If true, it forces re-fetching the URL.
* @param {string} [options.html] HTML string to be used instead of fetching
* the requested URL.
* @param {boolean} [options.replace] If true, it replaces the current entry in
* the browser session history.
* @param {number} [options.timeout] Time until the navigation is aborted, in
* milliseconds. Default is 10000.
*
* @return {Promise} Promise that resolves once the navigation is completed or
* aborted.
*/
export const navigate = async ( href, options = {} ) => {
const url = cleanUrl( href );
navigatingTo = href;
Expand Down

0 comments on commit 1cf650a

Please sign in to comment.