10000 [AssetMapper] Load es-module-shims only if importmap is not supported by nicolas-grekas · Pull Request #58121 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Load es-module-shims only if importmap is not supported #58121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

nicolas-grekas
Copy link
Member
@nicolas-grekas nicolas-grekas commented Aug 29, 2024
Q A
Branch? 7.2
Bug fix? no
New feature? no
Deprecations? no
Issues -
License MIT

Let's not load the polyfill if the browser already supports importmaps

@newQuery
Copy link
newQuery commented Aug 30, 2024

I'd suggest to create two separate methods formatAttributes & generateAttributeScript to simplify the code

if ($polyfillPath) {
    $polyfillAttributes = array_merge([
        'src' => $polyfillPath,
        'async' => true,
    ], $attributes, $this->scriptAttributes);

    // Add security attributes if using the default polyfill
    if ($polyfillPath === self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL) {
        $polyfillAttributes = array_merge([
            'crossorigin' => 'anonymous',
            'integrity' => self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY,
        ], $polyfillAttributes);
    }

    $output .= '<script async' . $this->formatAttributes($scriptAttributes) . '>
        if (!HTMLScriptElement.supports("importmap")) {
            (function() {
                const script = document.createElement("script");
                ' . $this->generateAttributeScript($polyfillAttributes) . '
                document.head.appendChild(script);
            })();
        }
    </script>';
}

The methods:

private function formatAttributes(array $attributes): string
{
    $formatted = '';
    foreach ($attributes as $name => $value) {
        $formatted .= ' ' . htmlspecialchars($name, \ENT_NOQUOTES) . '="' . htmlspecialchars($value, \ENT_NOQUOTES) . '"';
    }
    return $formatted;
}

private function generateAttributeScript(array $attributes): string
{
    $script = '';
    foreach ($attributes as $name => $value) {
        $name = addslashes(htmlspecialchars($name, \ENT_NOQUOTES));
        $value = addslashes(htmlspecialchars($value, \ENT_NOQUOTES));
        $script .= "\n    script.setAttribute('{$name}', '{$value}');";
    }
    return $script;
}

Improve code readability, maintainability, and reusability

@newQuery
Copy link

I would also allow developers to configure whether es-module-shims should be loaded, providing more flexibility

@nicolas-grekas nicolas-grekas force-pushed the am-lazy-shim branch 2 times, most recently from 603cbe3 to 0461579 Compare August 30, 2024 13:16
@nicolas-grekas
Copy link
Member Author

I'd suggest to create two separate methods formatAttributes & generateAttributeScript to simplify the code

Factorized: I leveraged the already existing method createAttributeString

I would also allow developers to configure whether es-module-shims should be loaded, providing more flexibility

Not sure this would provide any benefits. Options are complexity. Anyway, that's a discussion for another PR/ issue.

<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support -->
<script async src="$url"$polyfillAttributes></script>
<script async$scriptAttributes>
if (!HTMLScriptElement.supports || !HTMLScriptElement.supports('importmap')) (function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed this works using Browerstack :shipit:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon in Nicolas' bio "GPT+BrowserStack powered JS dev" 😁

@nicolas-grekas nicolas-grekas merged commit dafb531 into symfony:7.2 Aug 30, 2024
9 of 10 checks passed
@nicolas-grekas nicolas-grekas deleted the am-lazy-shim branch August 30, 2024 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0