Skip to content

Commit

Permalink
Merge pull request #46 from adenvt/release/0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
adenvt authored Jul 14, 2023
2 parents fc7876c + 060c00f commit eb56d38
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 274 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nupolyon",
"version": "0.2.0",
"version": "0.2.1",
"packageManager": "yarn@3.6.1",
"description": "Auto-Inject polyfill from polyfill.io",
"license": "MIT",
Expand Down Expand Up @@ -41,8 +41,9 @@
"@nuxt/test-utils": "^3.1.2",
"@types/polyfill-library": "^3.108.1",
"changelogen": "^0.5.0",
"cheerio": "^1.0.0-rc.12",
"eslint": "^8.33.0",
"nuxt": "^3.1.2",
"vitest": "^0.32.0"
"vitest": "^0.33.0"
}
}
4 changes: 2 additions & 2 deletions src/runtime/server/plugins/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export default defineNitroPlugin((nitroApp) => {
const host = !isSelfHost ? src : cleanDoubleSlashes(joinURL(config.app.baseURL, src))

if (host) {
const preload = `<link rel="preload" href="${host}" crossorigin="anonymous" as="script" />`
const preload = `<link rel="preload" href="${host}" crossorigin="anonymous" as="script" data-testid="nupolyon-preload" />`
// NOTE: We intentionally omit type="module" here because it will defer the execution of the polyfill
const polyfill = `<script src="${host}" crossorigin="anonymous"></script>`
const polyfill = `<script src="${host}" crossorigin="anonymous" data-testid="nupolyon-script"></script>`

nitroApp.hooks.hook('render:html', (html) => {
html.head.unshift(preload, polyfill) // insert at the beginning of the array
Expand Down
11 changes: 9 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { describe, it, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, fetch, $fetch } from '@nuxt/test-utils'
import cheerio from 'cheerio'

describe('ssr', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
})

it('should render the index page', async () => {
it('should inject the polyfill script into page', async () => {
// Get response to a server-rendered page with `$fetch`.
const html = await $fetch('/')
expect(html).toContain('<div>basic</div>')
const $ = cheerio.load(html)

const preload = $('link[data-testid="nupolyon-preload"]')
const script = $('script[data-testid="nupolyon-script"]')

expect(preload.attr('href')).toBe('/_nupolyon/polyfill')
expect(script.attr('src')).toBe('/_nupolyon/polyfill')
})

it('should return selfhosted polyfill with correct mimetype', async () => {
Expand Down
Loading

0 comments on commit eb56d38

Please sign in to comment.