Skip to content

[feature]: add fetch hook #1757

@zhoushaw

Description

@zhoushaw

Clear and concise description of the problem

In the new version of the runtime sdk, we added the manifest concept. If the user wants to request the manifest file with some header information or retry if the request fails, the current one lacks similar handling.fetch is also called for script resource requests in MF SSR, and hooks should apply here as well

Suggested solution

async function fetchWithRetry(url, options = {}, retries = 3) {
    try {
        const response = await fetch(url, options);

        if (!response.ok) {
            throw new Error(`Server error:${response.status}`);
        }

        return response;
     } catch (error) {
        if (retries <= 0) { 
            throw new Error('The request failed three times and has now been abandoned');
        }

        // If there are remaining times, delay 1 second and try again
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log(`Trying again. Number of retries available:${retries-1}`);
        return await fetchWithRetry(url, options, retries - 1);
    }
}  


function RetryPlugin () {
    return {
        name: "retry-plugin",
        async fetch(url, config){
            return fetchWithRetry(url, config)
        }
    }
}


init({
    name: "@demo/host",
    remotes: [],
    pluigns: [RetryPlugin()]
});

Alternative

No response

Additional context

No response

Validations

  • Read the Contributing Guidelines.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions