Passed
Push — develop ( 059ea9...a64540 )
by Andrew
04:50
created

ViteService::manifestScript()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
cc 10
eloc 35
c 9
b 0
f 1
nc 36
nop 4
dl 0
loc 52
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2021 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\pluginvite\services;
12
13
use nystudio107\pluginvite\helpers\FileHelper;
14
use nystudio107\pluginvite\helpers\ManifestHelper;
15
16
use Craft;
17
use craft\base\Component;
18
use craft\helpers\Html as HtmlHelper;
19
use craft\helpers\Json as JsonHelper;
20
use craft\web\View;
21
22
use yii\base\InvalidConfigException;
23
use yii\caching\TagDependency;
24
25
use Throwable;
26
27
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @package   Vite
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
30
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
31
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
32
class ViteService extends Component
33
{
34
    // Constants
35
    // =========================================================================
36
37
    const VITE_CLIENT = '@vite/client';
38
    const LEGACY_POLYFILLS = 'vite/legacy-polyfills';
39
40
    // Public Properties
41
    // =========================================================================
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var bool Should the dev server be used for?
45
     */
46
    public $useDevServer;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var string File system path (or URL) to the Vite-built manifest.json
50
     */
51
    public $manifestPath;
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @var string The public URL to the dev server (what appears in `<script src="">` tags
55
     */
56
    public $devServerPublic;
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var string The public URL to use when not using the dev server
60
     */
61
    public $serverPublic;
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @var string The JavaScript entry from the manifest.json to inject on Twig error pages
65
     *              This can be a string or an array of strings
66
     */
67
    public $errorEntry = '';
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var string String to be appended to the cache key
71
     */
72
    public $cacheKeySuffix = '';
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @var string The internal URL to the dev server, when accessed from the environment in which PHP is executing
76
     *              This can be the same as `$devServerPublic`, but may be different in containerized or VM setups.
77
     *              ONLY used if $checkDevServer = true
78
     */
79
    public $devServerInternal;
80
81
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @var bool Should we check for the presence of the dev server by pinging $devServerInternal to make sure it's running?
83
     */
84
    public $checkDevServer = false;
85
86
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
87
     * @var bool Whether the react-refresh-shim should be included
88
     */
89
    public $includeReactRefreshShim = false;
90
91
    // Protected Properties
92
    // =========================================================================
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
95
     * @var bool Whether the manifest shims has been included yet or not
96
     */
97
    protected $manifestShimsIncluded = false;
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
100
     * @var bool Whether the dev server shims has been included yet or not
101
     */
102
    protected $devServerShimsIncluded = false;
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @var bool Whether the modulepreload polyfill has been included yet or not
106
     */
107
    protected $modulepreloadPolyfillIncluded = false;
108
109
    // Public Methods
110
    // =========================================================================
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
113
     * @inheritDoc
114
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
115
    public function init()
116
    {
117
        parent::init();
118
        // Do nothing for console requests
119
        $request = Craft::$app->getRequest();
120
        if ($request->getIsConsoleRequest()) {
121
            return;
122
        }
123
        // Our component is lazily loaded, so the View will be instantiated by now
124
        if (Craft::$app->getConfig()->getGeneral()->devMode) {
125
            Craft::$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
126
        }
127
    }
128
129
    /**
130
     * Return the appropriate tags to load the Vite script, either via the dev server or
131
     * extracting it from the manifest.json file
132
     *
133
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
135
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
136
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
137
     *
138
     * @return string
139
     */
140
    public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
141
    {
142
        if ($this->devServerRunning()) {
143
            return $this->devServerScript($path, $scriptTagAttrs);
144
        }
145
146
        return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
147
    }
148
149
    /**
150
     * Return the script tag to load the script from the Vite dev server
151
     *
152
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
153
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
154
     *
155
     * @return string
156
     */
157
    public function devServerScript(string $path, array $scriptTagAttrs = []): string
158
    {
159
        $lines = [];
160
        // Include any dev server shims
161
        if (!$this->devServerShimsIncluded) {
162
            // Include the react-refresh-shim
163
            if ($this->includeReactRefreshShim) {
164
                $script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix);
165
                // Replace the hard-coded dev server URL with whatever they have theirs set to
166
                $script = str_replace(
167
                    'http://localhost:3000/',
168
                    rtrim($this->devServerPublic, '/') . '/',
169
                    $script
170
                );
171
                $lines[] = HtmlHelper::script(
172
                    $script,
173
                    [
174
                        'type' => 'module',
175
                    ]
176
                );
177
            }
178
            $this->devServerShimsIncluded = true;
179
        }
180
        // Include the entry script
181
        $url = FileHelper::createUrl($this->devServerPublic, $path);
182
        $lines[] = HtmlHelper::jsFile($url, array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
183
            'type' => 'module',
184
        ], $scriptTagAttrs));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
185
186
        return implode("\r\n", $lines);
187
    }
188
189
    /**
190
     * Return the script, module link, and CSS link tags for the script from the manifest.json file
191
     *
192
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
193
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
194
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
195
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
196
     *
197
     * @return string
198
     */
199
    public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string
200
    {
201
        $lines = [];
202
        ManifestHelper::fetchManifest($this->manifestPath);
203
        $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
204
        $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
205
        // Include any manifest shims
206
        if (!$this->manifestShimsIncluded) {
207
            // Handle any legacy polyfills
208
            if (!empty($legacyTags)) {
209
                $lines[] = HtmlHelper::script(
210
                    FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix),
211
                    []
212
                );
213
                $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
214
                $tags = array_merge($legacyPolyfillTags, $tags);
215
            }
216
            $this->manifestShimsIncluded = true;
217
        }
218
219
        foreach (array_merge($tags, $legacyTags) as $tag) {
220
            if (!empty($tag)) {
221
                $url = FileHelper::createUrl($this->serverPublic, $tag['url']);
222
                switch ($tag['type']) {
223
                    case 'file':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
224
                        $lines[] = HtmlHelper::jsFile($url, $tag['options']);
225
                        break;
226
                    case 'css':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
227
                        $lines[] = HtmlHelper::cssFile($url, $tag['options']);
228
                        break;
229
                    case 'import':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
230
                        $lines[] = HtmlHelper::tag('link', '', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
231
                            'crossorigin' => $tag['crossorigin'],
232
                            'href' => $url,
233
                            'rel' => 'modulepreload',
234
                        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
235
                        break;
236
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
237
                        break;
238
                }
239
            }
240
        }
241
242
        if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modulepreloadPolyfillRequired seems to be never defined.
Loading history...
243
            $this->modulepreloadPolyfillIncluded = true;
244
            $lines[] = HtmlHelper::script(
245
                FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix),
246
                ['type' => 'module']
247
            );
248
        }
249
250
        return implode("\r\n", $lines);
251
    }
252
253
    /**
254
     * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or
255
     * extracting it from the manifest.json file
256
     *
257
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
258
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
259
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
260
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
261
     *
262
     * @return void
263
     * @throws InvalidConfigException
264
     */
265
    public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [])
266
    {
267
        if ($this->devServerRunning()) {
268
            $this->devServerRegister($path, $scriptTagAttrs);
269
270
            return;
271
        }
272
273
        $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
274
    }
275
276
    /**
277
     * Register the script tag to the Craft View to load the script from the Vite dev server
278
     *
279
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
280
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
281
     *
282
     * @return void
283
     * @throws InvalidConfigException
284
     */
285
    public function devServerRegister(string $path, array $scriptTagAttrs = [])
286
    {
287
        $view = Craft::$app->getView();
288
        // Include any dev server shims
289
        if (!$this->devServerShimsIncluded) {
290
            // Include the react-refresh-shim
291
            if ($this->includeReactRefreshShim) {
292
                $script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix);
293
                // Replace the hard-coded dev server URL with whatever they have theirs set to
294
                $script = str_replace(
295
                    'http://localhost:3000/',
296
                    rtrim($this->devServerPublic, '/') . '/',
297
                    $script
298
                );
299
                $view->registerScript(
300
                    $script,
301
                    $view::POS_HEAD,
302
                    [
303
                        'type' => 'module',
304
                    ],
305
                    'REACT_REFRESH_SHIM'
306
                );
307
            }
308
            $this->devServerShimsIncluded = true;
309
        }
310
        // Include the entry script
311
        $url = FileHelper::createUrl($this->devServerPublic, $path);
312
        $view->registerJsFile(
313
            $url,
314
            array_merge(['type' => 'module'], $scriptTagAttrs),
315
            md5($url . JsonHelper::encode($scriptTagAttrs))
316
        );
317
    }
318
319
    /**
320
     * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file
321
     *
322
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
323
     * @param bool $asyncCss
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
324
     * @param array $scriptTagAttrs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
325
     * @param array $cssTagAttrs
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
326
     *
327
     * @return void
328
     * @throws InvalidConfigException
329
     */
330
    public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [])
331
    {
332
        $view = Craft::$app->getView();
333
        ManifestHelper::fetchManifest($this->manifestPath);
334
        $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
335
        $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs);
336
        // Include any manifest shims
337
        if (!$this->manifestShimsIncluded) {
338
            // Handle any legacy polyfills
339
            if (!empty($legacyTags)) {
340
                $view->registerScript(
341
                    FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix),
342
                    $view::POS_HEAD,
343
                    [],
344
                    'SAFARI_NOMODULE_FIX'
345
                );
346
                $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
347
                $tags = array_merge($legacyPolyfillTags, $tags);
348
            }
349
            $this->manifestShimsIncluded = true;
350
        }
351
        foreach (array_merge($tags, $legacyTags) as $tag) {
352
            if (!empty($tag)) {
353
                $url = FileHelper::createUrl($this->serverPublic, $tag['url']);
354
                switch ($tag['type']) {
355
                    case 'file':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
356
                        $view->registerJsFile(
357
                            $url,
358
                            $tag['options'],
359
                            md5($url . JsonHelper::encode($tag['options']))
360
                        );
361
                        break;
362
                    case 'css':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
363
                        $view->registerCssFile(
364
                            $url,
365
                            $tag['options']
366
                        );
367
                        break;
368
                    case 'import':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
369
                        $view->registerLinkTag(
370
                            [
371
                                'crossorigin' => $tag['crossorigin'],
372
                                'href' => $url,
373
                                'rel' => 'modulepreload',
374
                            ],
375
                            md5($url)
376
                        );
377
                        break;
378
                    default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
379
                        break;
380
                }
381
            }
382
        }
383
384
        if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modulepreloadPolyfillRequired seems to be never defined.
Loading history...
385
            $this->modulepreloadPolyfillIncluded = true;
386
            $view->registerScript(
387
                FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix),
388
                $view::POS_HEAD,
389
                ['type' => 'module'],
390
                'MODULEPRELOAD_POLYFILL'
391
            );
392
        }
393
    }
394
395
    /**
396
     * Return the contents of a local file (via path) or remote file (via URL),
397
     * or null if the file doesn't exist or couldn't be fetched
398
     * Yii2 aliases and/or environment variables may be used
399
     *
400
     * @param string $pathOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
401
     * @param callable|null $callback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
402
     *
403
     * @return string|array|null
404
     */
405
    public function fetch(string $pathOrUrl, callable $callback = null)
406
    {
407
        return FileHelper::fetch($pathOrUrl, $callback, $this->cacheKeySuffix);
408
    }
409
410
    /**
411
     * Determine whether the Vite dev server is running
412
     *
413
     * @return bool
414
     */
415
    public function devServerRunning(): bool
416
    {
417
        // If the dev server is turned off via config, say it's not running
418
        if (!$this->useDevServer) {
419
            return false;
420
        }
421
        // If we're not supposed to check that the dev server is actually running, just assume it is
422
        if (!$this->checkDevServer) {
423
            return true;
424
        }
425
        // Check to see if the dev server is actually running by pinging it
426
        $url = FileHelper::createUrl($this->devServerInternal, self::VITE_CLIENT);
427
428
        return !($this->fetch($url) === null);
429
    }
430
431
    /**
432
     * Invalidate all of the Vite caches
433
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
434
    public function invalidateCaches()
435
    {
436
        $cache = Craft::$app->getCache();
437
        TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix);
438
        Craft::info('All Vite caches cleared', __METHOD__);
439
    }
440
441
    // Protected Methods
442
    // =========================================================================
443
444
    /**
445
     * Inject the error entry point JavaScript for auto-reloading of Twig error
446
     * pages
447
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
448
    protected function injectErrorEntry()
449
    {
450
        // If there's no error entry provided, return
451
        if (empty($this->errorEntry)) {
452
            return;
453
        }
454
        // If it's not a server error or a client error, return
455
        $response = Craft::$app->getResponse();
456
        if (!($response->isServerError || $response->isClientError)) {
457
            return;
458
        }
459
        // If the dev server isn't running, return
460
        if (!$this->devServerRunning()) {
461
            return;
462
        }
463
        // Inject the errorEntry script tags to enable HMR on this page
464
        try {
465
            $errorEntry = $this->errorEntry;
466
            if (is_string($errorEntry)) {
0 ignored issues
show
introduced by
The condition is_string($errorEntry) is always true.
Loading history...
467
                $errorEntry = [$errorEntry];
468
            }
469
            foreach ($errorEntry as $entry) {
470
                $tag = $this->script($entry);
471
                if ($tag !== null) {
472
                    echo $tag;
473
                }
474
            }
475
        } catch (Throwable $e) {
476
            // That's okay, Vite will have already logged the error
477
        }
478
    }
479
}
480