Войдите или зарегистрируйтесь
Вы сможете писать комментарии и посты, ставить лайки и другое
Поиск
Тёмная тема

Nuxt 3 and 4: issue with multiple navigateTo calls

20 дн. назад
At work, on one of our projects, I ran into an issue in Nuxt 4 where a page component needs to perform different redirects via navigateTo() based on various conditions during the execution of the setup function. Something like this:

<template>
  Some page
</template>

<script lang="ts" setup>
if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
}

if (2 + 2 === 4) {
  await navigateTo('/?abc=2', {
    redirectCode: 302,
  });
}

if (3 + 3 === 6) {
  await navigateTo('/?abc=3', {
    redirectCode: 302,
  });
}

if (4 + 4 === 8) {
  await navigateTo('/?abc=4', {
    redirectCode: 302,
  });
}
</script>

This is where the problem appears: in the end, the redirect always goes to the very last URL, i.e. in the example above the navigation ends up at /?abc=4. I dug into the documentation but couldn't find anything built-in to prevent subsequent navigateTo calls from being triggered. It seems this behavior is related to vue router.

You can't write it like this either:
if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
  return; // not allowed in script setup
}

In the end, the only solution that worked was introducing a boolean variable and checking it before calling navigateTo:
let isAlreadyRedirected = false;

if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 2 + 2 === 4) {
  await navigateTo('/?abc=2', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 3 + 3 === 6) {
  await navigateTo('/?abc=3', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 4 + 4 === 8) {
  await navigateTo('/?abc=4', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}
Once again: this is the only thing that solved the problem for me. If you have any other solution, please let me know.
Показать полностью...
+2
3

The Same Typeface in Woody Allen's Films

14 дн. назад
In his films, Woody Allen consistently uses the same typeface in the opening and closing credits. Midnight in Paris (2011):
Показать полностью...
+2
4

How to use Quill Editor with Nuxt 3 and SSR (Vue)

недавно
If you try to use Quill Editor with Nuxt 3 when rendering a page in SSR you will get this error:
500 document is not defined.
That means NodeJS doesn't have the global variable 'document'. Because SSR rendering is executed in
a NodeJS environment, not a browser.

The sad fact: <client-only> won't help with this problem. The problem with quill's code is that during import, it assumes it's being executed in the browser. I hope you remember that the code imported from the module is not just imported, but executed, i.e. the authors of Quill wrote it so that the 'document' object is immediately accessed there. Very bad.

One solution is to disable SSR, but its an awful solution. But the second solution is to use dynamic JS imports.

My <script lang="ts" setup> in QuillEditor.vue in Nuxt 3 project:
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
// import Quill from 'quill'; // SSR problem with 'document', need to use dynamic import
import type Quill from 'quill';

const elemForQuillEditor = ref<HTMLDivElement|null>(null);

let editor: null|Quill = null; // Do not store in ref() - it causes bugs!

// ...

async function createQuillAndSetListeners() {
  const Quill = (await import('quill')).default; // This is most important thing - usage of JS dynamic import

  if (!elemForQuillEditor.value) return;
  editor = new Quill(elemForQuillEditor.value, {
    theme: 'snow',
    modules: {
      history: {
        delay: 2000,
        maxStack: 500,
        userOnly: true,
      },
      toolbar: {
        container: [
          ['bold', 'italic', 'underline', 'strike'],
          ['link'],
          [{ list: 'ordered' }, { list: 'bullet' }],
          [{ script: 'sub' }, { script: 'super' }],
          [{ header: [1, 2, 3, 4, 5, 6, false] }],
          [{ color: [] }, { background: [] }],
          ['clean'],
          ['undo', 'redo'],
        ],
        handlers: {
          undo() {
            editor?.history.undo();
          },
          redo() {
            editor?.history.redo();
          },
        },
      },
    },
    placeholder: props.placeholder,
  });
  
  editor.on('text-change', () => {
    if (!editor) return;
    // ... my other code
  });
}

// ...

onMounted(createQuillAndSetListeners);

Now your Quill editor will work in Nuxt 3 even during SSR!
+2
63

The foreman at the dock from The Naked Gun made it into the third film (actor Joe Grifasi)

недавно
Remember that memorable interrogation scene on Pier 32 from The Naked Gun?
The actor who played the sleazy dock foreman is named Joe Grifasi.
Показать полностью...
+4
11

Perfect Homemade Chicken Broth

недавно
For years, I avoided clear broths. Childhood memories told me they were bland and only meant for sick days. Then I tried this recipe — and everything changed.
It’s crystal clear, gently aromatic, and full of flavor.
Ingredients

1 chicken breast, skin on

2 liters (about 8 cups) water

3 allspice berries

3 whole black peppercorns

2 sprigs fresh parsley

½ tablespoon salt, level
Показать полностью...
+5
17

A dried-up future New York in The Fifth Element

недавно
In The Fifth Element, there's a brief and easily overlooked scene in the middle of the movie where a spaceship takes off toward Fhloston Paradise. It only lasts a couple of seconds, but if you pause and take a closer look, you'll notice an interesting detail from the director.
I've numbered the key spots in the screenshot.

What we see is New York of the future. The ocean level has dropped dramatically, making the city look almost unrecognizable. The only question is - where could all the water have gone for sea levels to fall that much?

1 - The Statue of Liberty. It looks taller because of the extended base underneath.
2 - This marks the future ocean level. Somehow the ice caps didn't melt to add more water to the seas. Did humanity ship the water off to other planets?

3 - Manhattan, 4 - Brooklyn, 5 - The Brooklyn Bridge connecting them. With the water gone, the bridge is now spanning a canyon instead of a river.

And on what used to be the ocean floor, they've built a spaceport.
Показать полностью...
+3
16

The real altitude of spaceflight

недавно
Humans have been flying to space since 1961 - that's over 60 years now. These days, we even have tourist spaceflights. In sci-fi movies, people travel between planets, stars, and even galaxies.

But what is space from the perspective of aviation and astronautics? Technically, it starts somewhere between 100 and 122 kilometers above Earth's surface. The range exists because different organizations define the boundary of space slightly differently.

Is 120 kilometers really that high? The average diameter of our planet is 12,742 kilometers - quite a lot. Below, I drew a simple picture to show what a spaceflight looks like to scale:
See that tiny red dot? That’s our trip to "outer space". Not exactly impressive, right? For comparison, the International Space Station orbits at an altitude of about 418.2 km - on this scale, its point would only be a couple of pixels higher.

Human technology is still in its infancy. Legally, we can say we fly to space - but in reality, it looks more like what you see above. As for interplanetary travel, so far, only our robots have made the journey.
+2
43

An argument against moon landing conspiracy theorists

недавно
I have a friend who believes in the moon landing conspiracy - you know, one of those people who think the Americans never actually went to the Moon.

He's convinced the whole thing was staged, masterfully filmed by movie directors. Arguing with him is almost pointless - he says every photo is fake. The fact that even Soviet scientists never questioned the authenticity of the American Moon landing doesn't matter to him.

But there's one argument that made him stop and think. No, I didn't manage to change his mind, but at least I got him to shut up for a while - confused him, so to speak.

The argument went like this: "So what, they faked all their Moon landings?"
Funny thing is, he didn't even know that the Americans went to the Moon not once, but several times.

Here's the list of all Moon missions:
Apollo 11 - July 16, 1969
Apollo 12 - November 14, 1969
Apollo 14 - January 31, 1971
Apollo 15 - July 26, 1971
Apollo 16 - April 16, 1972
Apollo 17 - December 7, 1972

So according to the "logic" of Moon landing deniers, the Americans didn't fake it just once - they supposedly faked it six times, along with all the photos, videos, and even the Moon rocks they brought back.
+2
33

Copenhagen - the city without lamp posts

недавно
Did you know that there are almost no lamp posts in Copenhagen? Open Google Maps and see for yourself.
Instead of posts, the city uses lamps suspended over the middle of the road.
Показать полностью...
+2
9

How wind turbine power depends on blade length: looking at real data

недавно
Wind turbines keep getting bigger every year. There's one main reason for that: the longer the blades, the more powerful the turbine. But couldn't you just install two smaller turbines instead of one large one? To answer this, let's take a look at real-world data.

Goldwind is a Chinese wind turbine manufacturer. Here are photos of their operating turbines from the company website:
Goldwind GW 82 / 1500 - output: 1500 kW (1.5 MW). Rotor length: 82.3 meters, meaning a blade diameter of roughly 41.15 meters.

Goldwind GW 171 / 6000 - output: 6000 kW (6.0 MW). Rotor length: 171 meters, giving a blade diameter of about 85.5 meters.

Even here you can already see that the relationship between blade length and output is nonlinear. The second turbine's blade is 2.07x longer, but the power is 4x higher!

Vestas is a wind turbine manufacturer from Denmark (their turbines are shown in the next photo):
Показать полностью...
+2
10